Understating MVC Application Start and Various Components

Purpose

We will learn how a request is processed in the code and various activities that happen in any MVC application start up. Every time we come across these but never see the basics of them. For example when a request comes, how are the routes filled and how is an area request handled in our application. When, how and why bundles are handled. These activities are done within a very important event handler that is called in a MVC application on the Application Start event.

I have taken a default template of MVC Web API. I will debug and walk you through and we will learn many basic concepts of MVC at the application start of a request processing and their implementation that is implemented in every MVC project.

As we know on an HTTP request from a client's browser comes to the web server and then finally though the MVC framework until in the Global.asax.cs a WebApiApplication object that implements HttpApplication.

Let's start in detail and do some debugging and I will walk you through the entire code. See the following:

entire code

1. Area (Organization application on the functional segment)

Since AreaRegistration is an abstract class with a static method, when we create an Area in our MVC project, it creates an AreaNameAreaRegistration.cs file. Let us understand it by looking in the class implementing this abstract class in our project.

AreaNameAreaRegistration

And it finds only one call implementing the AreaRegistration abstract class and overriding both the necessary and the abstract property AreaName and the abstract method RegisterArea for registring the routes. As we can see in the following, the AreaRegistration class has been implemented automatically by our area.

If you have multiple Areas in your application, all of them in the application will be registered one by one before execution going back to the Global.asax.cs.

Areas in your application

Global

As you can see, the context name is being set from the AreaName property and will create the route for the HelpPage Area that exists in the Area folder.

AreaName

Now the route is added as you can see in the preceding screen, in the route collection at index 6.

After this, execution again comes to the Global.asax.cs, to the next line to execute.

2. GlobalConfiguration.Configure(WebApiConfig.Register);

Please refer to this: Configuring ASP.NET Web API 2.

If you go to the GlobalConfiguration static class:

GlobalConfiguration

And then to the Action that is a delegate.

delegate

So, this method Configure of GlobalConfiguration takes a delegate as input (generic template) and as we can see it is of type HttpConfiguration.

So in our case, WebApiConfig.Register is the input to configure:

WebApiConfig

As you can see here, both classes exist, RouteConfig.cs and WebApiConfig.cs, in the App_Start Folder.

Now, let's go to WebApiConfig.Register that does all the Web API configuration as well as routing for the WebApi.

This is the current WebAPI Config code:

WebAPI Config

Route Config code:

Route Config code

Importing everthing to see in the WebAPI and that it is:

  1. not using {action} in the routeTemplate as in RouteConfig ulr.

    Because Web API routing decides the action automatically and uses the verbs (like get/post) to identify it. In MVC routing an action is required to get to the action.

  2. One using config.Routes.MapHttpRoute and another using config.Routes.MapHttpRoute.

    Since both are using a different HttpConfiguration in the WebAPI and MapHttpRoute in MVC objects.

    After this the execution returns to the next line of Global.asax.cs.

3. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

Registers the filter

In the default template case, the only filter added to GlobalFilterCollection is for HandleErrorAttribute. This FilterConfig class is in the App_Start folder.

For details check a quick and interesting conversation/article on this: Basics of Filters in MVC.

4. BundleConfig.RegisterBundles(BundleTable.Bundles);

This concept is called bundling and Minification.

Bundling bundles or combines or merges all the files into a single file. The files to be bundled are registered here in the BundleConfig class in the App_Start folder, in the method RegisterBundles. These files are added in BundelCollection and is loaded with a single request to the server.

This avoids multiple requests to the server for different CSS and JavaScript files.
.
different CSS

As you can see, once you execute the breakpoint line it will add the path for the Script Bundle.

Similarly on the next break point, as modernizer:

breakpoint

Once all the Bundles are added to the collection you can see them with their paths.

Once the response is posted, on the next request the Application_Start is not called and the Registration is not done again.

3. Next

Then comes:

  1. RouteConfig.RegisterRoutes(RouteTable.Routes);  
This class is also in the App_Start Folder and calls RegisterRoutes to fill the RouteTable "Routes" that is a RouteCollection.

This is a vast an important topic Routing in MVC. You can find many articles on this topic that explains it well.

I will stop here since this has became a long and lengthy article. In the next article I will write in detail on a single topic.

Up Next
    Ebook Download
    View all
    Learn
    View all