(一) 路由解析

1.  Default.aspx页面代码如下:
        public void Page_Load(object sender, System.EventArgs e)
        {
            // Change the current path so that the Routing handler can correctly interpret
            // the request, then restore the original path so that the OutputCache module
            // can correctly process the response (if caching is enabled).

            string originalPath = Request.Path;
            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);
        }

请求被重写为“/”,从而使设置的起始页路径被重定向

2.不解析Default.aspx(Web)页面

 RouteTable.Routes.RouteExistingFiles = false;如果找不到相应的控制器,则页面会报错,正如将这里的值改为true。

3. 改为下述代码则不会报错,因为指定了页面的路由解析是正确的。路由按顺序解析,如果解析错误就会报错

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            RouteTable.Routes.RouteExistingFiles = true;
            // RouteMonitor.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                 routes.MapRoute(
                       "Start",
                       "Default.aspx",
                        new { controller = "Home", action = "Index", id = "" }
                       ); 
          

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

      
    }

4. 添加 RouteMonitor.dll文件,用于监测路由表信息,该文件在资源文件中存在,代码如下:

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            RouteTable.Routes.RouteExistingFiles = true;
            RouteMonitor.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

     /*      routes.MapRoute(
                "Start",
                "Default.aspx",
                 new { controller = "Home", action = "Index", id = "" }
                ); 
       */

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

      
    }

5. 路由解析执行顺序

 用户请求(UrlRouting)

 路由检索(RouteTable)

 匹配路由

 路由解析(Route)

 路由处理程序(IRouteHandler)

 HTTP处理程序(IHttpHandler)

响应请求

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值