首页 新闻 会员 周边

areas中ActionLink或者RouterLink怎么生成http://domain/List/1/page1这样的连接?

0
悬赏园豆:20 [已解决问题] 解决于 2015-05-05 17:08

情况如下:areas中有admin和newscontent两个区块,其中new中router设置如下:

 public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "NewsContent_default",
                "NewsContent/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
            context.MapRoute(
                null,
                "",
                new { controller = "Newss", action = "List", category = (string)null, page = 1 }
                );

            context.MapRoute(
                null,
                "Page{page}",
                new { controller = "Newss", action = "List", category = (string)null },
                new { page = @"\d+" }
                );

            context.MapRoute
                (
                "null", // 路由名称
                "{category}", // 带有参数的 URL
                new { controller = "Newss", action = "List", page = 1 } // 参数默认值
            );
            context.MapRoute(
               null,
               "{category}/Page{page}",
               new { controller = "Newss", action = "List" },
               new { page = @"\d+" }
               );

        }

 

也就是访问http://domain/category/page 时候是访问了http://domain/Newscontent/Newss/list/category/page

 但是在http://domain/Newscontent/Newss/index 导航菜单中用RouterLink只能生成如http://domain/NewsContent/Newss/List?category=3&page=1 的连接

代码如下

<% foreach (var link in Model) { %> 
    <%: Html.RouteLink(link.Text, link.RouteValues, new Dictionary<string, object> { 
           { "class", link.IsSelected ? "selected" : null}  })%>
<% } %>

RouterValues如下:

RouteValues =  new RouteValueDictionary(new
            {
                controller = "Newss",
                action = "List",
                category = categoryName,
                page = 1
            }), 

我想生成如http://domain/category/page的连接,需要怎么做?

mzwkkk的主页 mzwkkk | 初学一级 | 园豆:125
提问于:2013-01-17 17:27
< >
分享
最佳答案
0

你把最后两个路由的顺序互换试试。

收获园豆:20
today4king | 老鸟四级 |园豆:3499 | 2013-01-17 17:35

不是路由的问题,用

<%: Html.RouteLink(link.Text, link.RouteValues, new Dictionary<string, object> {
           {
"class", link.IsSelected ? "selected" : null}  })%>

生成的连接就是http://domain/NewsContent/Newss/List?category=3&page=1的形式,如果生成http://domain/NewsContent/Newss/List/3/page1这样的也行

mzwkkk | 园豆:125 (初学一级) | 2013-01-17 17:50

我错的,确实是路由的问题,还是对route的原理还有mvc的运行过程理解不透彻,routelink生成链接的时候就已经调用route来生成的。context.MapRoute去匹配的时候是根据参数来匹配,如果把context.MapRoute(
                "NewsContent_default",
               
"NewsContent/{controller}/{action}/{id}",
               
new { action = "Index", id = UrlParameter.Optional }
            );
和context.MapRoute(
                "NewsContent_default",
               
"NewsContent/{controller}/{action}/{id}",
               
new { action = "Index", id = UrlParameter.Optional }
            );
放在前边,尽管后边还有category=3和page=1的参数,但是在第一个规则里边已经可以匹配了。所以后边就显示category=3&page=1的形式

mzwkkk | 园豆:125 (初学一级) | 2013-01-18 11:21

@mzwkkk: 问题解决了就好了

today4king | 园豆:3499 (老鸟四级) | 2013-01-18 18:03
其他回答(1)
0

@Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null)

@Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null)

万事俱备就差个程序员 | 园豆:229 (菜鸟二级) | 2014-05-25 16:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册