Asp.net 属性路由MVC 5中出错

Asp.net 属性路由MVC 5中出错,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc Routing,我正在尝试使用多个可选参数路由操作,但它不起作用。我正在分享我的代码,请引导我 [HandleError] [RouteArea("Admin", AreaPrefix = "sp-admin")] [RoutePrefix("abc-system")] [Route("{action}")] public class AbcController : Controller { [Route("list/{id:int?}/{PersonID?}/{ref?}")] public as

我正在尝试使用多个可选参数路由操作,但它不起作用。我正在分享我的代码,请引导我

[HandleError]
[RouteArea("Admin", AreaPrefix = "sp-admin")]
[RoutePrefix("abc-system")]
[Route("{action}")]
public class AbcController : Controller
{
   [Route("list/{id:int?}/{PersonID?}/{ref?}")]
   public async Task<ActionResult> Index(int? id, int? PersonID, string @ref)
   {
      return view();
   }
}
这样不行 但是像这样工作

我希望它的工作,如果链接有任何可选参数。
请告诉我这不起作用,因为路由不知道将int参数放在哪里。不过你可以这样做

[Route("list/{type}/{id}/{ref?}")]
public async Task<ActionResult> Index(string type, int id, string @ref)
{ 
    if(type == "Person"){ ... }
    else { ... }

   return View();
}

您可以指定“alpha”作为@ref action参数的约束,并有如下两种操作:

[Route("list/{id:int?}/{ref:alpha?}")]
public async Task<ActionResult> Index(int? id, string @ref)
{
  return await Index(id, null, @ref);
}

[Route("list/{id:int?}/{personId:int?}/{ref:alpha?}")]
public async Task<ActionResult> Index(int? id, int? personId, string @ref)
{
  return View();
}
这两种情况都适用。我更喜欢这个,因为我不必这样做
一次又一次地修改我的路线。

路线怎么知道你想要2个id和personID?这根本无法做到。@有什么方法可以做到这一点吗?您可以试试这个,或者您可以将这两条路线添加到同一个操作中。但我认为你应该修改你的行动方法设计,而不是试图把所有的选择放在一个。
[Route("list/{id:int?}/{ref:alpha?}")]
public async Task<ActionResult> Index(int? id, string @ref)
{
  return await Index(id, null, @ref);
}

[Route("list/{id:int?}/{personId:int?}/{ref:alpha?}")]
public async Task<ActionResult> Index(int? id, int? personId, string @ref)
{
  return View();
}