[英]ASP.NET MVC routing: with querystring to one action, without to another
我需要以下路线:
example.com/products
转到产品类别页面(例如汽车,卡车,公共汽车,自行车)
控制器= Products
,操作= Categories()
example.com/products?a=1&b=2
进入特定类别中所有产品的索引(例如,福特,本田,雪佛兰)
控制器= Products
,操作= Index(string a, string b)
路由仅在查询字符串上有所不同,似乎MVC会忽略“?”之后的任何内容。 因此,当然只有一条规则会受到打击-第一条。
如何区分两者?
编辑:换句话说,我想要两条路线。 可以在路由中使用查询字符串,还是MVC真正忽略它? 有什么办法可以破解它,或者使用某种自定义路由方案,就像我可以执行自定义绑定和自定义验证一样?
介绍参数。 ASP.NET MVC允许您创建“漂亮的” URL,这正是您应该在此处执行的操作:
首先,路由映射:
routes.MapRoute(
"SpecificProducts",
"products/{a}/{b}",
new { controller = "products", action = "Categories" }
);
routes.MapRoute(
"ProductsIndex",
"products".
new { controller = "products", action = "Index" }
);
然后,控制器动作
public ActionResult Index()
{
}
public ActionResult Categories(string a, string b) //parameters must match route values
{
}
这将使您能够使用易于搜索的URL,而不必担心查询字符串参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.