ASP.NET WebAPI 中的异常 [RoutePrefix] 行为

Unusual [RoutePrefix] behavior in ASP.NET WebAPI

我有两个控制器如下:

[RoutePrefix("v1/user/something")]
public class SomethingsController : ApiController
{
    [Route("{id}")]
    [HttpGet]
    [ResponseType(typeof(SomethingsViewModel))]
    public async Task<IHttpActionResult> GetAsync([FromUri]int id)
    {
    }
}

[RoutePrefix("v1/user")]
public class UserController : ApiController
{
    [Route("{id}")]
    [HttpGet]
    [Authorize(Roles = "Super Admin")]
    public async Task<IHttpActionResult> GetByIdAsync([FromUri]int id)
    {
    }
}

现在通过查看上面的代码,我认为正在创建以下两条路由:

但不幸的是,出于某种原因,情况并非如此。尝试访问上述路线之一时,我不断收到以下异常消息:

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL. The request has found the following matching controller types: MyProject.Api.Controllers.UserController, MyProject.Api.Controllers.SomethingsController

请帮助我弄清楚我可能做错了什么或者我在这里遗漏了哪些小细节。

虽然它们的路由前缀不同,但它们解析的路由匹配。例如 v1/user/{id} 将匹配 v1/user/something/{id},其中第一个路由中的 id 参数 arg 将采用 something/{id}

路由前缀和路由属性结合创建添加到路由 table 的完整路由。

在这种情况下,您将需要使用约束以更好地区分路线。

[RoutePrefix("v1/user/something")]
public class SomethingsController : ApiController {
    [Route("{id:int}")]
    [HttpGet]
    [ResponseType(typeof(SomethingsViewModel))]
    public async Task<IHttpActionResult> GetAsync([FromUri]int id) { ... }
}

[RoutePrefix("v1/user")]
public class UserController : ApiController {
    [Route("{id:int}")]
    [HttpGet]
    [Authorize(Roles = "Super Admin")]
    public async Task<IHttpActionResult> GetByIdAsync([FromUri]int id) { ... }
}

所以现在有了 int 约束 something 就不会被误认为是 UserController.GetByIdAsync 操作的有效参数

引用Attribute Routing in ASP.NET Web API 2: Route Constraints

Route Constraints

Route constraints let you restrict how the parameters in the route template are matched. The general syntax is "{parameter:constraint}". For example:

[Route("users/{id:int}"]
public User GetUserById(int id) { ... }

[Route("users/{name}"]
public User GetUserByName(string name) { ... }

Here, the first route will only be selected if the "id" segment of the URI is an integer. Otherwise, the second route will be chosen.