更改默认路由映射后,我的actionlinks停止工作

时间:2013-05-18 14:24:30

标签: asp.net-mvc razor asp.net-mvc-routing actionlink

我对路线图进行了更改,现在正在关注:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{title}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }
            );

这是不再有效的动作链接:

@Html.ActionLink("Category", "CategoryList", "Category")

当我点击此动作链接时,网页重新加载时,网址保持不变http://localhost:62394/。它太奇怪了

当我检查html时,它看起来像这样:

<a href="">Category</a>

任何形式的帮助或提示都很受欢迎!

注意:如果我从路由中移除标题,它就可以工作......

1 个答案:

答案 0 :(得分:1)

这是因为你有2个可选参数,所以路由引擎不知道将第三个参数映射到哪一个。感觉标题将用于特定路线而不是通用路线。如果是这种情况,为什么不为它创建一个特定的路由并将其从通用后备路由中删除?

这样的事情:

routes.MapRoute(
            name: "Title",
            url: "Category-List/{title}",
            defaults: new { controller = "Category", action = "CategoryList", title = UrlParameter.Optional }
        );
routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );