VictorStone

导航

 
先注册路由
            routes.MapRoute(
                name: "Member",
                url: "Member/{action}/{page}",
                defaults: new { controller = "MemberCenter", action = "List", id = UrlParameter.Optional },
                constraints: new { action = "index|list|detail", page = "\d+" }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
注意路由约束是完全匹配的,比如:page = "\d+",实际执行时会转换为: page = "^\d+$"来匹配。

到视图文件中加入以下内容测试:
<p>
    @RouteTable.Routes.GetVirtualPath(
        Request.RequestContext,
            new RouteValueDictionary(new
            {
                controller = "MemberCenter",
                action = "detail"
            })
    ).VirtualPath;
</p>

这里只能匹配第二个(第一个要求page参数):/MemberCenter/detail;

这次加入一个带page参数的试试:

        RouteTable.Routes.GetVirtualPath(
            Request.RequestContext,
                new RouteValueDictionary(new
                {
                    controller = "MemberCenter",
                    action = "Detail",
                    page = "TEST"
                })
        ).VirtualPath

/MemberCenter/Detail?page=TEST

需要特别注意的是如果把第二注册的参数改为page = UrlParameter.Optional会导致路由解析重叠覆盖,解析结果:

/MemberCenter/detail; /MemberCenter/Detail/TEST 都是后注册的第二条

如果加一个链接: @Html.ActionLink("测试链接", "detail", "MemberCenter", new { page = "testpage" }, null);  始终指向 http://localhost:44622/MemberCenter/detail/testpage

 

posted on 2014-12-19 09:51  VictorStone  阅读(189)  评论(0编辑  收藏  举报