本文介绍了具有多个可选参数的asp.net mvc路由不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个属性route Route("Home/{category?}/{Subcategory?}/List")
想要匹配以下示例/Home/C1/S1/List
,/Home/C1/List
,/Home/List
I wrote a attribute route Route("Home/{category?}/{Subcategory?}/List")
want to match the following examples/Home/C1/S1/List
, /Home/C1/List
, /Home/List
但是仅为了匹配第一个URL,可选参数不起作用.如何使用匹配上述三个示例的路由规则?
But only to match the first url, the optional parameter did not work.How to use a routing rule matches the above three examples?
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[Route("Home/{category?}/{subcategory?}/List")]
public IActionResult List(Category? category = null, SubCategory? subcategory = null)
{
return Content(category.ToString() + "/" + subcategory.ToString());
}
[Route("Home/{code}/Detail")]
public IActionResult Detail(string code)
{
return Content(code);
}
}
public enum Category
{
C1,
C2,
C3,
C4
}
public enum SubCategory
{
S1,
S2,
S3,
S4
}
类似问题
推荐答案
如果路由不起作用,则可以使用三个路由规则来完成.
If a route does not work, it can be done using three routing rules.
[Route("Home/List")]
[Route("Home/{category}/List")]
[Route("Home/{category}/{subcategory}/List")]
全部.
这篇关于具有多个可选参数的asp.net mvc路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!