zoukankan      html  css  js  c++  java
  • ASP.NET MVC 路由规则写法

       看书的时候,喜欢去思考,喜欢问自己几个为什么,但是天资愚笨,长时间找不到答案,这可如何是好?上天呀,赐给我一个聪明的大脑吧!或者告诉我如何在遇到问题的时候,能快速的解决。这篇博客不为别的,只是提供一种解决问题的方法,作为程序员,虽然我算不上,源码可能才是问题的本质。

      新创建一个ASP.NET.MVC项目的时候,会有一个默认的路由规则,形式如下:

    routes.MapRoute(
     "Default", // 路由名称
     "{controller}/{action}/{id}", // 带有参数的 URL
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
    );
    

     我们知道大括号里面的表示占位符,既然是占位符,那么换成其它的是不是也行,当换成 

    routes.MapRoute(
                    "Default", // 路由名称
                    "{action}/{id}", // 带有参数的 URL
                    new { controller = "Home",action = "LogOn", id = UrlParameter.Optional } // 参数默认值
                );

    这个是可以的,但是换成下面的就不行,结果出错。

    routes.MapRoute(
                    "Default", // 路由名称
                    "{action}/{id}", // 带有参数的 URL
                    new { action = "LogOn", id = UrlParameter.Optional } // 参数默认值
                );

    错误信息也提示了,必须有"controller",对于我该的第一种情况,也就是下面的虽然在规则中没出现,但是默认值中确出现了,也就是说这个其实是还有 “controller”的

     routes.MapRoute(
                    "Default", // 路由名称
                    "{action}/{id}", // 带有参数的 URL
                    new { controller = "Home",action = "LogOn", id = UrlParameter.Optional } // 参数默认值
                );
    

    决定找到答案,于是反编译了代码,看到了自己想要的答案:

     其实也可以从网上下载到源码,从源码中也可以看到:

       private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
            {
                // If request validation has already been enabled, make it lazy. This allows attributes like [HttpPost] (which looks
                // at Request.Form) to work correctly without triggering full validation.
                // Tolerate null HttpContext for testing.
                HttpContext currentContext = HttpContext.Current;
                if (currentContext != null)
                {
                    bool? isRequestValidationEnabled = ValidationUtility.IsValidationEnabled(currentContext);
                    if (isRequestValidationEnabled == true)
                    {
                        ValidationUtility.EnableDynamicValidation(currentContext);
                    }
                }
    
                AddVersionHeader(httpContext);
                RemoveOptionalRoutingParameters();
    
                // Get the controller type
                string controllerName = RequestContext.RouteData.GetRequiredString("controller");
    
                // Instantiate the controller and call Execute
                factory = ControllerBuilder.GetControllerFactory();
                controller = factory.CreateController(RequestContext, controllerName);
                if (controller == null)
                {
                    throw new InvalidOperationException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            MvcResources.ControllerBuilder_FactoryReturnedNull,
                            factory.GetType(),
                            controllerName));
                }
            }
    
    
     System.Web.Mvc.  MvcHandler 

    同理,Action也是必须出现的。

  • 相关阅读:
    信息安全系统设计基础第九周学习总结
    信息安全系统设计基础第八周期中总结
    信息安全系统设计基础第七周学习总结
    深入理解计算机系统第六章家庭作业之6.35 6.36
    深入理解计算机系统第二、三章家庭作业之2.64 2.65 3.66 3.68
    信息安全系统设计基础第六周学习总结
    信息安全系统设计基础第五周学习总结
    JAVA 之 多态 抽象 接口
    JAVA 之 继承
    java.lang 类String
  • 原文地址:https://www.cnblogs.com/zuiyirenjian/p/3061792.html
Copyright © 2011-2022 走看看