When a new MVC Web Application is created in Visual Studio a lot of files are added to the solution. The goal of this post is to provide a small overview of all these files, this overview should make easier to understand the MVC solution structure.
The files described here are the files created by default when you add a web project using the MVC template.
Create a a Web project:
Use the MVC Template:
This is the generated project:
ASP MVC Template Overview
Let’s start enumerating some created folders:
- Content: CSS files with view styles. (Bootstrap)
- Fonts: Some fonts required by the CSS files.
- Controllers: C# files with the code that is going to be run at the server side.
Now the rest of files and folders with some more details:
This folder contains *.cshtml files. The Razor engine generates the views of our web application out of these files. Typically we will have a view or a group of views for each controller. This folder also contains a Web.config don’t confuse this one with the Web.config at the root of the solution. Razor engine is configured here.
- _ViewStart.cshtml: Razor will execute the code of this file before rendering any page. It is possible to have an additional _ViewStart.cshtml in a Views subfolder, if so, before executing the controllers inside this subfolder, the code of the root _ViewStart.cshtml is executed first and then the code of the _ViewStart.cshtml inside the folder. Typically these files are used to specify the common layout of the website and the common layout of groups of Views.
- _Layout.cshtml: A layout used by _ViewStart.cshtml to declare the common layout of the website. Bundles common to all the views of the website are rendered here. (Bootstrap, jQuery, Modernizr).
- Error.cshtml: Page shown when an error occurs in the application.
Equivalent to App.config. It contains the generic configuration:
- Target Framework version.
- Entity Framework providers and connection strings
- AssemblyBinding, allows to specify the concrete version of a dependency that must be used.
E.g; The following configuration tell the application that all dependencies to the assembly with name System.Web.Mvc and versions between 184.108.40.206 and 220.127.116.11 shall be resolved using the version 18.104.22.168.
This file could also contains some additional framework configuration, third party dependencies, application defined configuration, etc.
Subclass of HttpApplication, contains the application start point. Areas are a way to group Views and Controllers within a MVC application. This call use reflection to find and register all the defined areas.
MVC allows to define configuration files for bundling, routing and filtering. All this configuration mechanisms work in a similar way: the target of the registration is passed as parameter to a static method that register stuff inside. These static methods are call here with the “context” of the current application. The content of these files will be explained with more detail on the next section.
This bundles can also be minified to shrink the network usage. For debug purposes it is possible to disable bundles minification:
BundleTable.EnableOptimizations = false;
StyleBundle for CSS.
Both classes are a constructor with a string to reference the bundle from the view and methods to add files or directories to the bundle:
This bundles can be used on the views just calling the Render method with the name of the Bundle to render.
Filters are classes that can be executed before and after the actions of our Controllers. The following filters are supported:
Filters can be executed for a simple controller but can also be executed for all of them. FilterConfig class has a static method with a global filter collection as parameter. Filters executed for all the controllers shall be registered in this container, by default the handle error filter is register here.
Routing enables the mapping of a URL to a specific action of a controller. It is possible also to pass part of the URL as parameters to the action.
With this I finish my overview of the ASP MVC template. Visual Studio gives a great start point to create an ASP MVC website with a good structure providing all these files just creating the project.