Sunday, July 12, 2015

The use of App_Start() folder in MVC4

App_start folder has been introduced in Mvc4. It contains various configurations files like as :
  • BundleConnfig.cs,
  • FilterConfig.cs,
  • RouteConfig.cs,
  • WebApiConfig.cs
for you application.All those settings are registered within App_Start method of Global.asax.cs file
BundleConfig.cs:
This is used to create and register bundles for CS and JS files.By default various bundles are added in this file including jQuery,jQueryUI,jQuery validation,Modernizr and Site Css..
FilterConfig.cs-
This is used to create and register global MVC filter error filter,action filter etc.By default it contains HandleErrorAttribute filter.
RouteConfig.cs-
This is used to register various route patterns for your Asp.Net MVC application. By default,one route is registered here named as Default Route.
WebApiConfig.cs-
This is used to register various WEB API routes like as Asp.Net MVC,as well sa set any addtionsl WEB ApI configurations settings.

  • AuthConfi g.cs: Used to confi gure security settings, including sites for OAuth login.
  • BundleConfi g.cs: Used to register bundles used by the bundling and minifi cation system. Several bundles are added by default, including jQuery, jQueryUI, jQuery validation, Modernizr, and default CSS references.
  • FilterConfi g.cs: Unsurprisingly, this is used to register global MVC fi lters. The only fi lter registered by default is the HandleErrorAttribute, but this is a great place to put other fi lter registrations.
  • RouteConfi g.cs: Holds the granddaddy of the MVC confi g statements, Route confi guration.
  • WebApiConfi g.cs: Used to register Web API routes, as well as set any additional Web API confi guration settings.
  • 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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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.
    1
    2
    3
    4
    5
    6
    7
    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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public static class WebApiConfig
    {
      public static void Register(HttpConfiguration config)
      {
        config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "api/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
        );
      }
    }

No comments:

Post a Comment