[英]What is the flow of ASP.NET Core Web API pipeline if I do not have Content-Type at all in my header?
I'm trying to solve this problem where if a client does not have content type in their header before even reaching my controller I want to display my OWN error message.我正在尝试解决这个问题,如果客户端在到达我的 controller 之前在其 header 中没有内容类型,我想显示我的 OWN 错误消息。 Not the [ApiController] generic problems details messages, but entirely my own JSON Object.
不是 [ApiController] 通用问题详细信息,而是我自己的 JSON Object。 I've been trying to figure this out and I at least know that having the [ApiController] attribute over my controller will give me problem details response for 4XX error codes.
我一直试图弄清楚这一点,我至少知道在我的 controller 上具有 [ApiController] 属性会给我 4XX 错误代码的问题详细信息响应。 I removed this and now I have empty response bodies.
我删除了这个,现在我有空的响应体。 I would like the same status code just a different body/object as the response.
我想要与响应相同的状态代码,只是不同的主体/对象。 What would be the best way to approach this?
解决这个问题的最佳方法是什么? I would like to know the flow so I can catch this error before hand and return my response with my own JSON object
我想知道流程,以便我可以提前发现这个错误并用我自己的 JSON object 返回我的响应
You can solve this using Middleware.您可以使用中间件解决此问题。 The following is a sample Middleware class that you can create and hook up to the HTTP Pipeline
以下是您可以创建并连接到 HTTP 管道的示例中间件 class
public class InterceptContentType
{
private RequestDelegate _next;
public InterceptContentType(RequestDelegate next)
{
this._next = next;
}
public async Task Invoke(HttpContext context)
{
// This is where you can intercept the content type as shown below
// and perform what you need
context.Request.Headers[HeaderNames.ContentType] = "application/xml";
await _next.Invoke(context);
}
}
Hooking up this class to your HTTP Pipeline is as simple as adding the below statement to your Configure() method within Startup.cs class将此 class 连接到 HTTP 管道就像将以下语句添加到 Startup.cs class 中的 Configure() 方法一样简单
app.UseMiddleware<InterceptContentType>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.