UrlHelper.Action方法没有使用第一个可变参数映射到正确的路径

时间:2014-08-01 16:20:58

标签: c# asp.net asp.net-mvc asp.net-mvc-4 routes

让我们看一下在RouteConfig.cs中映射的这两条路线,一条带有guid,一条没有:

// Non-guid route
routes.MapPageRoute(
    name: null,
    url: "dashboard/{action}",
    defaults: new { controller = "Dashboard", action = "Index" }
);
// Guid route
routes.MapPageRoute(
    name: null,
    url: "{guid}/dashboard/{action}",
    defaults: new { controller = "Dashboard", action = "Index" }
);

方法调用:

UrlHelper.Action("Index", "Dashboard", new { guid = "c73b47b9-4ad5-414a-a92e-8937231f8e2bD" })

该方法返回:

"/dashboard/?guid=c73b47b9-4ad5-414a-a92e-8937231f8e2b"

但是我期待着这个:

"/c73b47b9-4ad5-414a-a92e-8937231f8e2b/dashboard/"

请注意,它将guid作为查询字符串值而不是第一个参数。

现在,如果我硬编码第二个网址应用程序使用正确的guid路由,并且如果我使用路由名称(UrlHelper.RouteUrl)也可以工作,所以我不认为这是路由映射的问题他们自己。这些解决方案都不适用于我们的应用程序,因为我们有数百条路线,目前正在使用UrlHelper.Action。

我也尝试过UrlHelper.Action的所有重载和变体,包括传递RouteValueDictionary而不是匿名对象。

这是一个ASP.NET MVC 4 Web应用程序。

为什么这不起作用的任何想法? (或替代方案,可能使用一条路线)

1 个答案:

答案 0 :(得分:1)

试试这个。首先提出更具体的路线:

routes.MapRoute(
    name: "WithGUID",
    url: "{guid}/dashboard/{action}",
      defaults: new { controller = "Dashboard", action = "Index" }
);

routes.MapRoute(
    name: "NoGUID",
    url: "dashboard/{action}",
      defaults: new { controller = "Dashboard", action = "Index", guid = "" }
);
相关问题