简体   繁体   English

如果我的 Z099E7396E 中根本没有 Content-Type,ASP.NET 核心 Web API 管道的流程是什么?

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

相关问题 ASP.NET MVC应用程序不输出内容类型标头 - ASP.NET MVC Application not outputting content-type header ASP.NET 核心 2 - 缺少内容类型边界 - ASP.NET Core 2 - Missing content-type boundary InvalidOperationException:错误的内容类型:ASP.NET Core - InvalidOperationException: Incorrect Content-Type: ASP.NET Core 如何在 ASP.NET MVC 中使用 GET 请求发送 Content-Type 标头? - How can I send Content-Type header with a GET request in ASP.NET MVC? 删除 .NET Core 中的 Content-Type 标头 - Removing Content-Type header in .NET Core ASP.NET Web API 2.0 管道和 ASP.NET Core Web API 管道的区别 - ASP.NET Web API 2.0 pipeline and ASP.NET Core Web API pipeline differences 如何在我上传文件的Asp.Net核心web api端点上进行集成测试? - How to do an integration test on my Asp.Net core web api endpoint where I upload a file? Web应用程序和API AzureAD身份验证流程ASP.NET Core - Web Application and API AzureAD authentication flow ASP.NET Core 在.NET MVC 4(Web API)中,如何拦截控制器请求并更改其内容类型? - In .NET MVC 4 (Web API), how do I intercept a request for a controller and change it's Content-Type? ASP.Net Core 为 JSON 内容设置 Content-Type 和 Location 标头 - ASP.Net Core set Content-Type and Location headers for JSON content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM