我的动作链接停止工作

我的动作链接停止工作

本文介绍了更改默认路由图后,我的动作链接停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对路由映射进行了更改,该路由映射如下:

I made a change to my routing map which is following now:

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

这是不再起作用的操作链接:

And this is the actionlinks that are not working anymore:

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

当我单击该动作链接时,没有任何反应,URL保持与页面重新加载相同的http://localhost:62394/.太奇怪了

When I click on this actionlink nothing happens the url stays the same http://localhost:62394/ as the page reloads. its so weird

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

and when I check the html it looks like this:

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

任何帮助或提示都值得赞赏!

Any kind of help or tips is appreciate alot!

注意:如果我从路由中删除标题,则可以使用...

note: If I remove title from the routing it works...

推荐答案

这是因为您有2个可选参数,因此路由引擎不知道将第三个参数映射到哪个参数.感觉标题将用于特定的路径,而不是通用的路径.如果是这样,为什么不为它创建一个特定的路由并将其从通用后备路由中删除呢?

It's because you've got 2 optional parameters so the routing engine doesn't know which one to map the third parameter to. It feels like title is going to be used for a specific route rather than a generic one. If that's the case, why not create a specific route for it and remove it from the generic fallback route?

类似这样的东西:

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 }
        );

这篇关于更改默认路由图后,我的动作链接停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:49