【问题标题】:Mvc Action Link not using correct routeMvc 操作链接未使用正确的路由
【发布时间】:2012-02-29 20:51:15
【问题描述】:

我有以下路线,在浏览器中输入它们可以正常工作并且路线正确,但如果我使用 Html.ActionLink,它会尝试使用 DefaultStuff 路线。

路线

_routes.MapStuffRoute(
    "DefaultStuff",
    "stuff/{controller}/{id}",
    new { id = UrlParameter.Optional },
    new[] { typeof(BaseApiController).Namespace });

_routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { typeof(BaseController).Namespace });

页面

@Html.ActionLink("Job Queues", "Index", "Job") // generates http://localhost/stuff/job?action=Index

我缺少什么让 ActionLink 生成 http://localhost/stuff/index。反转 ActionLink 的路线是正确的,但 Stuff 不起作用。请注意,StuffRoute 根据请求中的信息设置操作名称。

【问题讨论】:

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


    【解决方案1】:

    似乎您实际上是在尝试将控制器“作业”映射到东西。当前,您的“DefaultStuff”路由无法解析操作,因此将其作为查询字符串值放入。

    _routes.MapRoute(
    "DefaultStuff",
    "stuff/{action}/{id}",
    new { controller="Job", id = UrlParameter.Optional },
    new[] { typeof(BaseApiController).Namespace });
    

    【讨论】:

    • 严格来说,对于要解析为路由的操作,该路由必须对应于一个操作,就像您的示例一样。在 OP 的示例中,{action}{controller} 替换,这将导致路由解析器失败
    • Action 是根据请求中的元数据在 GetRouteData 中动态设置的。
    • 看看路由调试器。它可能会帮助您解决此类问题:haacked.com/archive/2008/03/13/url-routing-debugger.aspx
    猜你喜欢
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2017-12-18
    • 2018-01-03
    • 2013-11-13
    • 2013-02-12
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多