[ASP.NET MVC 4] Application Start

The “Application_Start()” method in the “Global.asax” is the entry point of the MVC web application. Routes, areas, and global filters are initialized here. MVC 4 project templates provide a little bit different way of initializing these features.

1. MVC 3 Templates

If you look at the “Global.asax” file in MVC 3 templates, the “Application” class has static methods to initialize routes and filters.

public class MvcApplication : System.Web.HttpApplication
{
  public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
    filters.Add(new HandleErrorAttribute());
  }

  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
      "Default", // Route name
      "{controller}/{action}/{id}", // URL with parameters
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
  }

  protected void Application_Start()
  {
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
  }
}

At first glance, it is pretty clear what each method is intended for. But as the size of the application grows, the code becomes longer and longer.

The “Global.asax” file is the central place and you do want to make the file manageable at all cost. Also the main OOP principal is to make a class to do a single task.

 

2. MVC 4 Templates

Designers of MVC 4 also knew this issue and provides the better structure from the project template itself.

 

3. Global.asax in MVC 4

If you open the “Global.asax” file in the MVC 4 applications, the code becomes much succinct.

public class MvcApplication : System.Web.HttpApplication
{
  protected void Application_Start()
  {
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
  }
}

It does 4 tasks:

  • Register areas
  • Register Web Api configuration
  • Register global filters
  • Register routes

So, where are the initialization code now?

The template creates the “App_Start” folder and adds 3 class files:

  • FilterConfig.cs
  • RouteConfig.cs
  • WebApiConfig.cs

The initialization code is defined in the static method of each class.

 

4. Routes Initialization in MVC 4

The “RouteConfig” class defines the static “RegisterRoutes()” method.

public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
  }
}

By using the named arguments, the code becomes more readable.
 

5. Global Filters in MVC 4

The “FilterConfig” class defines the static “RegisterGlobalFilters()” method.

public class FilterConfig
{
  public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
    filters.Add(new HandleErrorAttribute());
  }
}

 

6. Web API Configuration in MVC 4

The “Web API” is a nice addition to ASP.NET and is available from MVC 4.

public static class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
    );
  }
}
Advertisements

One thought on “[ASP.NET MVC 4] Application Start

Leave a Reply

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