我正在实现一个api,其中在一个特定的POST路由上必须进行API密钥检查。
SDK是dotnet核心3.1。
使用Postman运行API时,出现以下错误:
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:该请求匹配了多个端点
我看起来api控制器上的route属性以及端点路由注册一起堆叠了...
如何解决?
public void Configure(IApplicationBuilder app, IHostEnvironment env)
{
app.UseSerilogRequestLogging();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger(Configuration);
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapApiKeyAuthentication();
endpoints.MapControllers();
});
}
(扩展类)
public static IEndpointRouteBuilder MapApiKeyAuthentication(this IEndpointRouteBuilder endpoints)
{
endpoints.MapPost("/v1/xyz", endpoints.CreateApplicationBuilder()
.UseMiddleware<ApiKeyMiddleware>()
.Build())
.WithDisplayName("ApiKey");
return endpoints;
}