2

I'm trying to figure out what the global.asax is and what the lines in it mean.

I understood the concept of the global file but I can't seem to figure out what the content of it means. This is the file:

Protected void Application_Start()
{   
   AreaRegistration.RegisterAllAreas();
   WebApiConfig.Register(GlobalConfiguration.Configuration);
   FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
   RouteConfig.RegisterRoutes(RouteTable.Routes);

} 

Also, I understood there is an importance to the order of these lines.

2 Answers 2

3

This is essentially the MVC frameworks bootstrap method to hook into the ASP.NET framework on startup. The Application_Start gets called when the application domain is loaded. You can edit this file as you see fit and depending on what you chose when you created your now project some of these lines may or may not be included by default. What you have there.

  • AreaRegistration.RegisterAllAreas(); - registers the areas, if you have an mvc application you can configure areas which are ways to further group functionality / views. See Areas for more detail.
  • WebApiConfig.Register(GlobalConfiguration.Configuration); - registers the Web API routing and additionally add global web api filters.
  • FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); - Add global mvc filters. See also previous SO question what this method does.
  • RouteConfig.RegisterRoutes(RouteTable.Routes); - initialize the mvc routing.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I understood it with both answers :)
@AmitToren - glad to hear it. Please also consider accepting an answer (see How to accept SO answers).
1

You will find a App_Start folder in your project as

 Protected void Application_Start()
 {   
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
 } 

Clearly see that each line denotes to each configuration files.

enter image description here

These files are used so as to register the respective configurations.

  1. BundleConfig -- is used to bundle and minify all your css and javascript code. To do so

    BundleTable.EnableOptimizations = true;

  2. FilterConfig

-- is used to register all your filters that can be used across controllers.This is the place where you will include your custom filters in the below method

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new customFilterAttribute());
        }
  1. RouteConfig - used for Registering your Routes for your application. I think this doesn't needs further explanation.

  2. WebApiConfig -- used in the case of WebApi is selected. Similar to your asp.net mvc routes. The web api config also has its own routes. Configurations like Serialization for json,xml are specified here For instance:

    enter image description here

These two lines solves the problem of case-sensitivity of your Web API with ur front end applications.

4 Comments

Do you really think this edit is what OP is looking for ?
Ha ha, nice tried! You should have to gain more knowledge about SO, after you edited 500 post, you won't get +2 on edits. And regarding answer there are many similar post on SO, you just googled it out. and/or look at answer give by Igor. that is what OP is looking for.
Moreover, you should also refer SO model Be nice
Thanks a lot. I understood it with both answers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.