MVC 路由可选字符串参数和难看的 url
MVC Routing optional string parameter and ugly urls
我在 4.5.1 上使用 mvc 5.2.3 并想要这些 url
- /Nike/Shoes
- /Nike/Shoes/运行
- /Nike/Shirts/2002342345
- /Nike/Shoes/运行/98765432234
我的控制器有:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, int? page, string subcategory = "")
并给出 /Nike/Shoes/运行 但没有 /Nike/Shoes 但 /Products?brand=Nike&category=Shoes
当我添加
[HttpGet]
[Route("{brand}/{category}/{page:int?}")]
public ActionResult Index(string brand, string category, int? page)
/Nike/Shoes/ 有效,但我也有 /Nike/Shoes/?subcategory=运行
和我的详细方法:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{productid:int:min(9000)}", Name="ProductDetails")]
public ActionResult Details(int productid)
给出:
The current request is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String, System.String, System.Nullable`1[System.Int32], System.String) on type shopmvc.Controllers.ProductsController
System.Web.Mvc.ActionResult Index(shopmvc.ViewModels.ProductsViewModel) on type shopmvc.Controllers.ProductsController
System.Web.Mvc.ActionResult Details(Int32) on type shopmvc.Controllers.ProductsController
所以我的产品 link 和 category/subcategory 路由有问题。或者我尝试达到它的方式:
<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>
您是否尝试过将 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`
例如/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 = "")
获得顶级通用级别,并从那里确定其余级别如何?
[HttpGet]
[Route("{brand}/{category}/{*subdivision}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, string subdivision)
然后确定细分的拆分是否包含更深的路径或产品 ID,并根据该选择调用正确的方法
我在 4.5.1 上使用 mvc 5.2.3 并想要这些 url
- /Nike/Shoes
- /Nike/Shoes/运行
- /Nike/Shirts/2002342345
- /Nike/Shoes/运行/98765432234
我的控制器有:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, int? page, string subcategory = "")
并给出 /Nike/Shoes/运行 但没有 /Nike/Shoes 但 /Products?brand=Nike&category=Shoes
当我添加
[HttpGet]
[Route("{brand}/{category}/{page:int?}")]
public ActionResult Index(string brand, string category, int? page)
/Nike/Shoes/ 有效,但我也有 /Nike/Shoes/?subcategory=运行
和我的详细方法:
[HttpGet]
[Route("{brand}/{category}/{subcategory}/{productid:int:min(9000)}", Name="ProductDetails")]
public ActionResult Details(int productid)
给出:
The current request is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index(System.String, System.String, System.Nullable`1[System.Int32], System.String) on type shopmvc.Controllers.ProductsController System.Web.Mvc.ActionResult Index(shopmvc.ViewModels.ProductsViewModel) on type shopmvc.Controllers.ProductsController System.Web.Mvc.ActionResult Details(Int32) on type shopmvc.Controllers.ProductsController
所以我的产品 link 和 category/subcategory 路由有问题。或者我尝试达到它的方式:
<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>
您是否尝试过将 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`
例如/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 = "")
获得顶级通用级别,并从那里确定其余级别如何?
[HttpGet]
[Route("{brand}/{category}/{*subdivision}", Name="ProductIndex")]
public ActionResult Index(string brand, string category, string subdivision)
然后确定细分的拆分是否包含更深的路径或产品 ID,并根据该选择调用正确的方法