MVC网址路由

 在默认的情况下,如果该网址可以对应到网站根目录下的实体文件,就不会通过MVC处理,而是将其交给ASP.NET执行。

如果设置 RouteTable.Routes.RouteExistingFiles = true; 则所有文件都会使用路由规则比对,
比对成功,用MVC处理,比对失败则返回给ASP.NET处理

一、把URL路由加入到HttpApplication处理管道
   在C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config中的web.config中已注册了UrlRouting模块
    <httpModules>
            <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
    </httpModules>
二、路由检查
    如果有匹配的路由,找到IRouteHandler,通过routeHandler的GetHttpHandler方法获取IHttpHandler
    UrlRoutingModule代码如下
    protected virtual void Init(HttpApplication application)
    {
        application.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);
    }

    public virtual void PostResolveRequestCache(HttpContextBase context)
    {
        RouteData routeData = this.RouteCollection.GetRouteData(context);
        if (routeData != null)
        {
            IRouteHandler routeHandler = routeData.RouteHandler;
            if (routeHandler == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoRouteHandler, new object[0]));
            }
            if (!(routeHandler is StopRoutingHandler))
            {
                RequestContext requestContext = new RequestContext(context, routeData);
                IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
                if (httpHandler == null)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, RoutingResources.UrlRoutingModule_NoHttpHandler, new object[] { routeHandler.GetType() }));
                }
                context.Items[_requestDataKey] = new RequestData { OriginalPath = context.Request.Path, HttpHandler = httpHandler };
                context.RewritePath("~/UrlRouting.axd");
            }
        }
    }

三、默认的路由
在Global.asax.cs
        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 = UrlParameter.Optional } // Parameter defaults
            );

        }
{resource},{*pathInfo}为占位符

四、为路由加上限制条件
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new { id = @"\d+" }
            );
 new { id = @"\d+" } 限制id的参数值必须为数字

五、网址路由如何生成MVC可识别的网址
@RouteTable.Routes.GetVirtualPath(Request.RequestContext, new RouteValueDictionary (new { page = 1 } )).VirtualPath
在界面上显示为:/home/index/2?page=1

加上一条新的路由规则
            routes.MapRoute(
                "DefaultPage", // Route name
                "{controller}/{action}/{id}/{page}"
              );

@RouteTable.Routes.GetVirtualPath(Request.RequestContext, new RouteValueDictionary (new { page = 1 } )).VirtualPath
在界面上显示为:/home/index/2/1

posted @ 2012-02-13 14:57  虎头  阅读(1929)  评论(1编辑  收藏  举报