Asp.net MVC路由问题 [英] Asp.net MVC routing problem

查看:61
本文介绍了Asp.net MVC路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我在asp.net mvc中有一个问题。

如何重定向:



route - controller / action

{品牌} -car-prices - > / PriceList / PriceByBrand(例如:丰田汽车价格)

{color} - {brand} -car-prices - > / PriceList / PriceByColorAndBrand {例如:green-toyota-car-prices}

{modelyear} -car-prices - > / PriceList / PriceByModelYear {例如2005-car-prices}

{modelyear} - {brand} -car-prices - > / PriceList / PriceByModelYearAndBrand {例如2005-toyota-car-prices}



当我路由时,所有路由重定向同一行动(页面)。

我该怎么办?



我尝试过:



 routes.MapRoute(
name: carprices
url: {brand} -car-prices / {page}
默认值: new {controller = PriceList,action = PriceByBrand,brand = UrlParameter.Optional,page = UrlParameter.Optional},
名称空间: new [] { MyProject.Controllers}
);

routes.MapRoute(
name: colorcarprices
url: {color} - {brand} -car-prices / {page}
默认值: new {controller = PriceList,action = PriceByColorAndBrand,color = UrlParameter.Optional,brand = UrlParameter.Optional,page = UrlParameter.Optional},
名称空间: new [] { MyProject.Controllers}
);

解决方案

你路线有一些问题







引用:路线 - 控制器/行动

{品牌} -car-prices - > / PriceList / PriceByBrand(例如:丰田汽车价格)

{color} - {brand} -car-prices - > / PriceList / PriceByColorAndBrand {例如:green-toyota-car-prices}

{modelyear} -car-prices - > / PriceList / PriceByModelYear {例如2005-car-prices}

{modelyear} - {brand} -car-prices - > / PriceList / PriceByModelYearAndBrand {例如2005-toyota-car-prices}







这里有2条路线 {品牌} -car-prices {modelyear} -car-prices

他们具有相同的结构,没有办法将它们分开。



同样的问题是 {color} - {brand} -car-prices {modelyear} - {brand} -car-prices



订单规则也很重要



当规则 {brand} -car-prices 首先匹配任何其他规则你提供的路线。所以它被执行。



您也可以查看这些链接



使用短划线输出值 [ ^ ]



MVC路由问题 - 试图收集剩余的余额路线中的连字符 [ ^ ]


< blockquote>路由规则的顺序是重要的...

规则的外观将在第一次适合时停止...

在大多数情况下,这意味着更长的规则(有相同的开口)应该在短的之前...

所以在你的两个规则之间切换......


Hi. I have a problem with rouing in asp.net mvc.
How can i redirect :

route - controller/action
{brand}-car-prices -> /PriceList/PricesByBrand (For example : "toyota-car-prices")
{color}-{brand}-car-prices -> /PriceList/PricesByColorAndBrand {for example : "green-toyota-car-prices"}
{modelyear}-car-prices -> /PriceList/PricesByModelYear {for example "2005-car-prices"}
{modelyear}-{brand}-car-prices -> /PriceList/PricesByModelYearAndBrand {for example "2005-toyota-car-prices"}

when i routing, all routes redirecting same action (page).
How can i do correct?

What I have tried:

routes.MapRoute(
              name: "carprices",
               url: "{brand}-car-prices/{page}",
              defaults: new { controller = "PriceList", action = "PricesByBrand ", brand= UrlParameter.Optional, page = UrlParameter.Optional },
              namespaces: new[] { "MyProject.Controllers" }
          );

            routes.MapRoute(
              name: "colorcarprices",
               url: "{color}-{brand}-car-prices/{page}",
              defaults: new { controller = "PriceList", action = "PricesByColorAndBrand ", color= UrlParameter.Optional, brand= UrlParameter.Optional, page = UrlParameter.Optional },
              namespaces: new[] { "MyProject.Controllers" }
          );

解决方案

You have some issue in routes



Quote:

route - controller/action
{brand}-car-prices -> /PriceList/PricesByBrand (For example : "toyota-car-prices")
{color}-{brand}-car-prices -> /PriceList/PricesByColorAndBrand {for example : "green-toyota-car-prices"}
{modelyear}-car-prices -> /PriceList/PricesByModelYear {for example "2005-car-prices"}
{modelyear}-{brand}-car-prices -> /PriceList/PricesByModelYearAndBrand {for example "2005-toyota-car-prices"}




Here take 2 routes {brand}-car-prices and {modelyear}-car-prices .
They are of same structure there is no way to separate them.

Same issue is with the {color}-{brand}-car-prices and {modelyear}-{brand}-car-prices

The order of rule is also important

when the rule {brand}-car-prices come first it match any other route you are providing. So it is executed.

You can check these links also

Route value with dashes[^]

MVC routing problem - trying to pick up the remainder after a hyphen in the route[^]


The order of routing rules is matter...
The look for the rule will stop on the first fit...
In most cases that means that longer rules (with the same opening) should come before the short one...
So switch between your two rules...


这篇关于Asp.net MVC路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆