Why is MvcApplication.RegisterRoutes defined as static?

I know that this can be stupid, but I would like the gurus to find out for me ... Why this method is defined as static.

public class MvcApplication : System.Web.HttpApplication { /* Why this method is declared as static? */ 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 = "" } // Parameter defaults ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } } 
+4
source share
4 answers

its static, because it doesn’t need to be a method directly related to class instances, but rather a method that can be used in a static context.

In other words, it only affects the "routes" parameter, it does not use any fields or members of the class, so it makes sense to make it static.

+7
source

The method is static because it can be (as Mark points out) ... but I think the real reason that the ASP.NET team makes it static (since you are trying to solve your question: 'why?') Should simplify the modular testing your routes.

Steve Sanderson's ASP.NET MVC Pro has a nice section (using helper methods) when testing your routes. And I think that the MVC Contrib project also has some helper methods for unit testing your routes.

HTHS
Charles

+3
source

It makes sense to me to make it static so that you can get the routes of (possibly) another application without having to instantiate the application class. You can then use these routes to create action urls, etc. From one application to another.

+1
source

You only need one route table, and you need the same one that is used throughout the application. Creating statics means that you get a global set of values ​​that are defined in one place.

0
source

All Articles