简体   繁体   English

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

[英]Unusual [RoutePrefix] behavior in ASP.NET WebAPI

I have two Controllers as follows: 我有两个控制器,如下所示:

[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)
    {
    }
}

Now by looking at the code above, I'd think that the following two routes are being created: 现在,通过查看上面的代码,我认为正在创建以下两条路线:

  • v1/user/something/{id} V1 /用户/事/ {ID}
  • v1/user/{id} V1 /用户/ {ID}

But unfortunately, for some reason, that is not the case. 但是不幸的是,由于某种原因,事实并非如此。 I keep getting the following exception message when trying to access one of the above routes: 尝试访问上述路由之一时,我始终收到以下异常消息:

Multiple controller types were found that match the URL. 找到与URL匹配的多种控制器类型。 This can happen if attribute routes on multiple controllers match the requested URL. 如果多个控制器上的属性路由与请求的URL匹配,则会发生这种情况。 The request has found the following matching controller types: MyProject.Api.Controllers.UserController, MyProject.Api.Controllers.SomethingsController 该请求找到了以下匹配的控制器类型:MyProject.Api.Controllers.UserController,MyProject.Api.Controllers.SomethingsController

Please help me out in figuring what I might be doing wrong or which small detail am I missing out here. 请帮助我确定我可能做错了什么,或者我在这里错过了哪些小细节。

Though their route prefix are different their resolved routes match. 尽管它们的路由前缀不同,但它们的解析路由匹配。 for example v1/user/{id} will match v1/user/something/{id} where id parameter arg in the first route will take something/{id} . 例如v1/user/{id}将与v1/user/something/{id}匹配,其中第一条路线中的id参数arg将采用something/{id}

Route prefix and Route attributes combine to create a full route that is added to the route table. 路由前缀和路由属性结合在一起以创建完整的路由,该路由已添加到路由表中。

In a case like this you will need to use constraints in order to better differentiate the routes. 在这种情况下,您将需要使用约束以更好地区分路线。

[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) { ... }
}

So now with the int constraint something wont be mistaken for valid parameter for the UserController.GetByIdAsync action 所以,现在与int约束something不会被误认为是有效的参数为UserController.GetByIdAsync行动

Reference Attribute Routing in ASP.NET Web API 2: Route Constraints ASP.NET Web API 2中的参考属性路由:路由约束

Route Constraints 路线约束

Route constraints let you restrict how the parameters in the route template are matched. 路由约束使您可以限制路由模板中参数的匹配方式。 The general syntax is "{parameter:constraint}". 通用语法为“ {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. 在此,只有在URI的“ id”段为整数的情况下才选择第一个路由。 Otherwise, the second route will be chosen. 否则,将选择第二条路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM