【发布时间】:2016-12-08 11:27:51
【问题描述】:
我需要同时支持基于查询参数的路由 (/api/models?id=1) 和基于路由的路由 (/api/models/1),同时仍允许明确访问模型集合 (/api/models)?
我的控制器看起来(有点)像这样:
[Route("/api/{controller}")]
public class ModelsController : Controller
{
[HttpGet]
public Models[] GetModels([FromQuery]QueryOptions queryOptions)
{
//...
}
[HttpGet("{id:int}")]
public Model Get([FromRoute] int id)
{
//...
}
[HttpGet("?{id:int}")]
public Model Get2Try1([FromQuery] int id)
{
//Fails with ": The literal section '?' is invalid.
//Literal sections cannot contain the '?' character."
//Which makes sense after some reading...
}
[HttpGet]
public Model Get2Try2([FromQuery] int id)
{
//Fails with "AmbiguousActionException: Multiple actions matched.
//The following actions matched route data and had all constraints satisfied:
//GetModels and Get2Try2"
//Which I think I understand as well...the absence of optional params
//means ambiguous routing...
}
[HttpGet] //What here?
public Model Get2Try3([FromQuery] int id) //and/or here?
{
}
}
我觉得应该有某种方法(使用声明性路由)来实现这一点。有没有人做过类似的事情?
此外,当前的代码库是 ASP.NET Core (RC1),不久将升级到 RTM/1.0。双方的细节可能相似,但对其中一个/两个都感兴趣。
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-routing model-binding .net-core