asp.net-core - ASP.NET Core 路由无法匹配路由参数,API 资源管理器中的路由参数重复
问题描述
有这个代码:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
[Route("/api/intakes/{intakeId}/employees/")]
[ApiController]
public class EmployeesController : ControllerBase
{
[HttpGet]
public async Task<IReadOnlyCollection<object>> GetEmployees([FromRoute]int intakeId)
{
return new[] { new { a = "asdsf", id = intakeId } };
}
}
我正在尝试访问GET /api/intakes/5/employees/
,但路由不匹配。
日志:
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:5000/api/intakes/5/employees/ - -
Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware: Trace: All hosts are allowed.
Microsoft.AspNetCore.Routing.Matching.DfaMatcher: Debug: 1 candidate(s) found for the request path '/api/intakes/5/employees/'
Microsoft.AspNetCore.Routing.Matching.DfaMatcher: Debug: Endpoint 'WebApplication3.EmployeesController.GetEmployees (WebApplication3)' with route pattern 'api/intakes/{intakeId}/employees' was rejected by complex segment '{intakeId}' for the request path '/api/intakes/5/employees/'
Microsoft.AspNetCore.Routing.Matching.DfaMatcher: Debug: Endpoint 'WebApplication3.EmployeesController.GetEmployees (WebApplication3)' with route pattern 'api/intakes/{intakeId}/employees' is not valid for the request path '/api/intakes/5/employees/'
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware: Debug: Request did not match any endpoints
奇怪的是,在 API 资源管理器中,我可以看到intakeId
这条路线的 2 个参数
解决方案
您可以尝试应用路由约束来限制参数intakeId
,如下所示。
[Route("api/intakes/{intakeId:int}/employees/")]
[ApiController]
public class EmployeesController : ControllerBase
{
推荐阅读
- linux - mod_jk 无法连接 Apache 和 tomcat
- javascript - 更新对象的 useState 数组中的对象值
- javascript - 如何计算开始日期和结束日期期间的周数?
- javascript - 从 Amazon S3 存储桶加载图像时遇到问题(不使用 URL)
- flutter - RenderBox 没有在 Flutter 中布局
- microsoft-graph-api - 如何使用 API 创建事件日历 Microsoft-GraphAPI
- c# - 我可以从 64 位 C# Winforms 应用程序调用 MAPI32.DLL > MAPISendMail 吗?
- intershop - 在后台获取国家名称和国家代码时出现 NullPtr 异常
- r - 仅选择最近的 POSIXlt 日期/时间
- testing - 我在哪里可以下载示例 siebel CRM 应用程序来练习自动化测试