我知道这可能很愚蠢,但希望大师为我澄清一下......为什么这个方法被定义为 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);
}
}
最佳答案
它是静态的,因为它不需要是与类的实例直接相关的方法,而是可以在静态上下文中使用的方法。
换句话说,它只影响参数“routes”,它不使用任何类字段或成员,因此将其设为静态是有意义的。
关于asp.net-mvc - 为什么 MvcApplication.RegisterRoutes 定义为静态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1606505/