ASP MVC – Template Overview

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:

New Web Project

Use the MVC Template:

MVC Template

This is the generated project:

MVC Template 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.
  • Scripts: JavaScript files. (JQuery)
  • 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:

Views

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.

Views\Shared folder

  •  _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.

Web.config

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 1.0.0.0 and 5.1.0.0 shall be resolved using the version 5.1.0.0.

image

This file could also contains some additional framework configuration, third party dependencies, application defined configuration, etc.

Global.asax

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.

AreaRegistration.RegisterAllAreas()

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.

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)

RouteConfig.RegisterRoutes(RouteTable.Routes)

BundleConfig.RegisterBundles(BundleTable.Bundles)

BundleConfig.cs

ASP MVC provides a way of defining bundles of JavaScript and CSS files to the views. In this file is where bundles are defined. An important advantage of bundles is that they can contain more than one file but the client only makes one server request per bundle.

This bundles can also be minified to shrink the network usage. For debug purposes it is possible to disable bundles minification:

BundleTable.EnableOptimizations = false;

MVC offers two different classes to register JavaScript and CSS files with different optimized ways of minimization.

ScriptBundle for JavaScript.

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:

bundle.Include(“file”)

bundle.IncludeDirectory(“relativeDirectory”)

This bundles can be used on the views just calling the Render method with the name of the Bundle to render.

System.Web.Optimization.Scripts.Render(“~/bundles/js”)

FilterConfig.cs

Filters are classes that can be executed before and after the actions of our Controllers. The following filters are supported:

  • Authorization
  • Action
  • Result
  • Exception

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.

RouteConfig.cs

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.

Advertisements

Your feedback is important...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s