加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 为什么MvcApplication.RegisterRoutes被定义为静

发布时间:2020-12-16 03:56:43 所属栏目:asp.Net 来源:网络整理
导读:我知道这可能很傻,但是希望大师为我澄清一下……为什么这个方法被定义为静态.. public class MvcApplication : System.Web.HttpApplication{ /* Why this method is declared as static? */ public static void RegisterRoutes(RouteCollection routes) { ro
我知道这可能很傻,但是希望大师为我澄清一下……为什么这个方法被定义为静态..

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”,它不使用任何类字段或成员,因此它是静态的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读