ASP。具有多个路由值的NET MVC URL路由

时间:2022-11-30 21:18:07

I am having trouble with Html.ActionLink when I have a route that takes more than one parameter. For example, given the following routes defined in my Global.asax file:

我的Html有问题。当我有一个包含多个参数的路由时。例如,给定以下在我的全局中定义的路由。asax文件:

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}.mvc/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

routes.MapRoute(
    "Tagging",
    "{controller}.mvc/{action}/{tags}",
    new { controller = "Products", action = "Index", tags = "" }
);

routes.MapRoute(
    "SlugsAfterId",
    "{controller}.mvc/{action}/{id}/{slug}",
    new { controller = "Products", action = "Browse", id = "", slug = "" }
);

The first two routes work without a problem, but when I try to create an action link to the third route using:

前两条路径可以正常工作,但是当我尝试创建到第三条路径的动作链接时,使用:

<%= Html.ActionLink(Html.Encode(product.Name), "Details", new { id = product.ProductId, slug = Html.Encode(product.Name) }) %>

I end up with a URL like [site-root]/Details/1?slug=url-slug whereas I would like the URL to be more like [site-root]/Details/1/url-slug

我得到了一个URL,比如[site-root]/Details/1?slug= URL -slug,而我希望URL更类似于[site-root]/Details/1/ URL -slug

Can anyone see where I am going wrong?

有人能看出我哪里出错了吗?

3 个解决方案

#1


59  

It is using the first route that is fully satisfied. Try putting your SlugsAfterId route above the Default one.

它使用的是第一条完全满意的路线。尝试将你的鼻涕虫路径放在默认路径之上。

It's basically going: "Check Default. Got an action? Yes. Got an id? Yes. Use this one and chuck any other parameters in the querystring."

它基本上是:“检查默认值。有一个动作吗?是的。有一个id吗?是的。使用这个参数并在querystring中删除任何其他参数。

As a side note, doing that will make your Default route redundant as you provide a default value for the slug parameter.

作为补充说明,这样做将使您的默认路由变得多余,因为您为slug参数提供了一个默认值。

#2


31  

Garry (above) is correct. You can use Mr. Haack's route debugger for MVC. It can help resolve routing issues by showing you which routes are hit and when.

加里(上图)是正确的。您可以使用Haack先生的MVC路径调试器。它可以通过显示哪些路由被击中以及何时被击中来帮助解决路由问题。

Here is the Blog Post. And here is the Zip File.

这是博文。这是Zip文件。

#3


8  

You could add a Constraint to your Routes that contain "id" since it's presumably only to accept a number. This way the first route will only match when the "id" is numeric, then it would make the second route for all other values. Then place the one that includes the {slug} at the top and everything should work correctly.

您可以向包含“id”的路由添加约束,因为它可能只接受一个数字。这样,第一个路由将只在“id”为数字时匹配,然后它将为所有其他值创建第二个路由。然后将包含{slug}的那个放在顶部,所有的东西都应该正确工作。

routes.MapRoute(
    "SlugsAfterId",
    "{controller}.mvc/{action}/{id}/{slug}",
    new { controller = "Products", action = "Browse", id = "", slug = "" },
    new { id = @"\d+" }
);

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}.mvc/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" },  // Parameter defaults
    new { id = @"\d+" }
);

routes.MapRoute(
    "Tagging",
    "{controller}.mvc/{action}/{tags}",
    new { controller = "Products", action = "Index", tags = "" }
);

#1


59  

It is using the first route that is fully satisfied. Try putting your SlugsAfterId route above the Default one.

它使用的是第一条完全满意的路线。尝试将你的鼻涕虫路径放在默认路径之上。

It's basically going: "Check Default. Got an action? Yes. Got an id? Yes. Use this one and chuck any other parameters in the querystring."

它基本上是:“检查默认值。有一个动作吗?是的。有一个id吗?是的。使用这个参数并在querystring中删除任何其他参数。

As a side note, doing that will make your Default route redundant as you provide a default value for the slug parameter.

作为补充说明,这样做将使您的默认路由变得多余,因为您为slug参数提供了一个默认值。

#2


31  

Garry (above) is correct. You can use Mr. Haack's route debugger for MVC. It can help resolve routing issues by showing you which routes are hit and when.

加里(上图)是正确的。您可以使用Haack先生的MVC路径调试器。它可以通过显示哪些路由被击中以及何时被击中来帮助解决路由问题。

Here is the Blog Post. And here is the Zip File.

这是博文。这是Zip文件。

#3


8  

You could add a Constraint to your Routes that contain "id" since it's presumably only to accept a number. This way the first route will only match when the "id" is numeric, then it would make the second route for all other values. Then place the one that includes the {slug} at the top and everything should work correctly.

您可以向包含“id”的路由添加约束,因为它可能只接受一个数字。这样,第一个路由将只在“id”为数字时匹配,然后它将为所有其他值创建第二个路由。然后将包含{slug}的那个放在顶部,所有的东西都应该正确工作。

routes.MapRoute(
    "SlugsAfterId",
    "{controller}.mvc/{action}/{id}/{slug}",
    new { controller = "Products", action = "Browse", id = "", slug = "" },
    new { id = @"\d+" }
);

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}.mvc/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" },  // Parameter defaults
    new { id = @"\d+" }
);

routes.MapRoute(
    "Tagging",
    "{controller}.mvc/{action}/{tags}",
    new { controller = "Products", action = "Index", tags = "" }
);