asp.net core headers caching issue

[亡魂溺海] 提交于 2021-01-29 02:50:56

问题


Here are the request headers from postman to my asp.net_core 2.0 API.

  • Content-Type: application/json
  • Accept: application/json

Response from my asp.net core application is an exception thrown in a string format. But the below response headers have been sent from the API to postman.

Cache-Control : no-cache, no-store, must-revalidate Pragma : no-cache Transfer-Encoding : chunked Content-Type : application/json; charset=utf-8 Age : 0 Server : Kestrel

Since the Request body is a string , and the Content-Type is set as application/json. Client is unable to parse it. I have added the below code in my API middleware startup class configure() method to disable Headers cache. But since the "Accept" header is application/json, the response header "content-type" is also set to application/json instead of text/plain . Please let me know what additional things have to be done to change the response header content-type to text/plain.

app.Use(
    next =>
    {
        return async context =>
        {
            context.Response.OnStarting(
            () =>
            {
                context.Response.Headers.Add("Cache-Control", "no-cache, no-store, 
                must-revalidate");
                context.Response.Headers.Add("Pragma", "no-cache");
                context.Response.Headers.Add("Age", "0");
                return Task.CompletedTask;
            });
            await next(context);
        };
    });

Thanks in advance .

来源:https://stackoverflow.com/questions/54898603/asp-net-core-headers-caching-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!