gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:46:01 25 4
gpt4 key购买 nike

我有两个 Controller 如下:

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

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

  • v1/user/something/{id}
  • v1/用户/{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}

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

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

[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.

关于c# - ASP.NET WebAPI 中异常的 [RoutePrefix] 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39591739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com