2015-05-20 49 views
3

我使用MVC 5.2.3 4.5.1,并希望这些URLMVC路由可选字符串参数和丑陋的网址

  • /耐克/鞋
  • /耐克/鞋/运行
  • /耐克/衬衫/ 2002342345
  • /耐克/鞋/跑步/ 98765432234

我的控制器有:

[HttpGet] 
[Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")] 
public ActionResult Index(string brand, string category, int? page, string subcategory = "") 

,并给出/耐克/鞋/运行,但没有/耐克/鞋,但/产品?品牌=耐克&类别=鞋

当我添加

[HttpGet] 
[Route("{brand}/{category}/{page:int?}")] 
public ActionResult Index(string brand, string category, int? page) 

/耐克/鞋/工程,但我也有/耐克/鞋/子类=运行

和我的个人信息的方法:

[HttpGet] 
[Route("{brand}/{category}/{subcategory}/{productid:int:min(9000)}", Name="ProductDetails")] 
public ActionResult Details(int productid) 

给出:

当前请求是采用以下的操作方法之间暧昧: System.Web.Mvc.ActionResult指数(System.String,System.String,System.Nullable`1 [System.Int32],系统.String)on shopmvc.Controllers.ProductsController System.Web.Mvc.ActionResult索引(shopmvc.ViewModels.ProductsViewModel)类型shopmvc.Controllers.ProductsController System.Web.Mvc.ActionResult类型shopmvc.Controllers上的详细信息(Int32) .ProductsController

因此,我的产品链接和类别/子类别路线有问题。或与我试图达到它的方式:

<a href="@Url.RouteUrl("ProductDetails", new { brand = item.Brand, category = item.Category, subcategory = item.SubCategory, productid = item.ID })">test</a> 
<a href="@Url.Action("Details", "Products", new { brand = item.Brand, category = item.Category, subcategory = item.SubCategory, productid = item.ID })"> 
<a href="@Url.Action("Index", "Products", new { brand = c.Brand, category = c.Category, subcategory = c.SubCategory })">@c.DisplayName</a> 

回答

1

如何获得顶级通用级别,并从那里确定其余的?

[HttpGet] 
[Route("{brand}/{category}/{*subdivision}", Name="ProductIndex")] 
public ActionResult Index(string brand, string category, string subdivision) 

然后确定您的细分分割含有更深的路径,或包含一个productId并调用基于这样的选择

+0

我的子类是一个可选参数,但似乎不起作用。 asteriks在url模板'* subcategory'中做了什么? –

+0

这可以防止模板进一步解析URL的其他部分(简要解释),可以找到更多[链接](https://msdn.microsoft.com/en-us/library/cc668201%28v=vs.140%29 .aspx) – Razieltje

+0

因此,您建议使用https://msdn.microsoft.com/en-us/library/cc668201%28v=vs.140%29.aspx#handling_a_variable_number_of_segments_in_a_url_pattern,然后手动解析单个httpget方法中的字符串。如果它以大于10000的重定向操作结束,并且在'/'类别上进行细化和分割等等,我不知道这是否是最好的方法。尽管谢谢你的建议。也许其他用户可以证明这种方法? –

2

您是否尝试订购从最具体到最通用的Global.asax路由?您的应用程序不知道检查路线的顺序,所以像brand/category/subcategory/nnnnn可能是页面nnnnn或产品nnnnn。

你需要检查这条路线第一:

[HttpGet] 
[Route("{brand}/{category}/{subcategory}/{productid:int:min(9000)}", Name="ProductDetails")] 
public ActionResult Details(int productid) 

那么这一个:

[HttpGet] 
[Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")] 
public ActionResult Index(string brand, string category, int? page, string subcategory = "") 

另外,使页面的路线是这样的,你可以修改路由:

`brand/category/subcategory/p-n` 

eg /Nike/Shoes/Running/p-2

编辑:如果你这样做会发生什么?任何小于9000的数字都会被解释为页面编号,尽管它可能会让用户感到困惑!

[HttpGet] 
[Route("{brand}/{category}/{subcategory}/{page:int?:max:8999}", Name="ProductIndex")] 
public ActionResult Index(string brand, string category, int? page, string subcategory = "") 
+0

我喜欢属性的路由在Global.asax的正确方法,我可以在attributerouting使用routeorder太。 http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx –

+0

将可选页面参数设置为最大8999可用于细节页面,但是打破了类别url(有和没有子类别),因为它不再是可选的int。 –

+0

你可以让它成为可选项,并且仍然设置最大值? '{page:int?:max:8999}'我认为你选择了可能不明确的路线。你最好改变路线,或许这样的页码有'p-nnnn'什么的。 –