asp.net-mvc - 为什么 MvcApplication.RegisterRoutes 定义为静态?

标签 asp.net-mvc static routing

我知道这可能很愚蠢,但希望大师为我澄清一下......为什么这个方法被定义为 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/

相关文章:

php - 如何将 .html 附加到 cakephp 中的所有 URL?

asp.net-mvc - 尽管没有 [Required] 属性,MVC3 仍然出现 'property field is required' 错误

javascript - 使用 django 将 STATIC_URL 传递给文件 javascript

C++:静态原语在程序退出时是否无效?

android - RecyclerView Adapter 中的静态和非静态 View 有什么区别?

node.js - 我使用 express for node.js 得到冒号和我的参数

java - 我们能否拥有类似于 RabbitMq 的 Apache Kafka 强大的路由能力?

asp.net-mvc - CSS 选择器 : last-child not working when added with a FOR loop

c# - Umbraco从7.2.8升级到7.3.0破坏了网站

c# - 如何使用 Razor 通过循环正确生成 Bootstrap 网格?