What template is executed in the Global.asax.cs file in ASP.NET MVC 4?

I have never come across this code scheme right here. Would anyone like to explain this to me? (Or is there even a sample here? Is there a reason why this was done so? What are the benefits of this?) I'm new to general programming, and this is very interesting to me:

Global.asax.cs

protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); //... RouteConfig.RegisterRoutes(RouteTable.Routes); //... } 

WebApiConfig.cs

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

RouteConfig.cs

 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 } ); } } 
+7
source share
2 answers

You can easily write code in your example as follows:

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); RouteTable.Routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } 

However, as the volume of the necessary configuration grows, it makes sense to break it into several logically connected pieces. ASP.NET MVC supports this quite well, and the default project template is simply trying to help you with this.

+5
source

This is not a model, as an example of the principle of single responsibility (SRP) . At Global.asax, we know about the high-level tasks that are required to configure, but we leave the implementation separate.

+5
source

All Articles