C# 没有为ApicController调用Midleware
ASP.NET Core 3.1项目有一个自定义中间件,它根据请求url域注入路由数据值C# 没有为ApicController调用Midleware,c#,asp.net-core,.net-core,asp.net-core-middleware,C#,Asp.net Core,.net Core,Asp.net Core Middleware,ASP.NET Core 3.1项目有一个自定义中间件,它根据请求url域注入路由数据值 public class IdentificationMiddleware { private readonly RequestDelegate _next; public IdentificationMiddleware(RequestDelegate next) { this._next = next ?? throw new ArgumentNullExcep
public class IdentificationMiddleware {
private readonly RequestDelegate _next;
public IdentificationMiddleware(RequestDelegate next)
{
this._next = next ?? throw new ArgumentNullException(nameof(next));
}
public async Task InvokeAsync(HttpContext context) {
var identification = ...;
if (!identification) {
context.abort();
} else {
var routeData = context.GetRouteData();
routeData.Values.Add("identification", identification);
}
await _next(context);
}
}
中间件已在启动中注册:
app.UseRouting();
app.UseMiddleware<IdentificationMiddleware>();
app.UseEndpoints(endpoints =>
{
});
为什么在api控制器中调用操作之前不调用中间件?有可能叫人来吗?如何修复它?您的
ApiAction()
方法签名中不需要[FromRoute]字符串标识吗?谢谢。这就不同了。
[Route("api/some")]
[ApiController]
public sealed class SomeApiController : ControllerBase
{
[HttpGet("apiAction")]
public async Task<IActionResult> ApiAction(
string identification,
[FromQuery] long id)
{
}
}
{
"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title":"One or more validation errors occurred.",
"status":400,
"traceId":"guid",
"errors":{
"identification":["The identification field is required."]
}
}