作者:moTzxx | 来源:互联网 | 2023-05-30 22:59
写了一个Attribute路由1Route("Home/{category?}/{subcategory?}/List")想匹配以下规则1/Home/C1/S1/List,1/Home/C1/List
写了一个Attribute路由
1
| Route("Home/{category?}/{subcategory?}/List") |
想匹配以下规则
,
,
好像只能匹配到第一种情况,可选参数没起作用。
如若匹配以上怎么用一个路由规则搞定?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| 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
} |
在stackoverflow的提问:asp.net mvc routing with multiple optional parameters did not work