Disable Cache-Control Header field in custom ASP.NET HttpHandler response

一个人想着一个人 提交于 2020-01-04 02:21:18

问题


How can I completely supress the output of the Cache-Control Header that is returned by my custom HttpHandler in ASP.NET?

I know, I can change the header field by modifying response.Cache and response.Cache.SetCacheability, but that will only change the header field, not remove it. That is not what I want. I completely want to make no assumptions about the Cache-Control field and leave it up to the browsers policy.

EDIT: The same holds true for HttpResponse.Charset. If no charset is set, ASP.NET will always set it to "utf-8", although returning a header "Content-Type: text/html" without the charset parameter is perfectly valid html. Any idea how to supress the charset, too?


回答1:


You can remove header values using

HttpContext.Current.Response.Headers.Remove("HEADER-VALUE-HERE"); 

If you are using a IIS7 integrated pipeline, however some header values are protected and cannot be removed (i.e they are injected after the response is dealt with on the most common page lifecycles).

What you can do if the above fails (ie its protected) is you can make a module to manually hook into the PreSendRequestHeaders and use the code above to remove the header at this point.

The majority of problems removing the header is not the inabillity to remove it, its to remove it at the correct time.

I believe using a custom http handler you should be able to remove it at the instantiation of the handler though.




回答2:


I'm only guessing here, but I would think that this is configured within IIS -- on the website (or virtual directory)'s Properties dialog, there is a section called "Enable content expiration". Does unticking this box suppress the header?

If you don't have access to your IIS Manager, then I'm afraid I am not sure how you could suppress that header. Looking at the docs, response.Headers is a read-only property, so setting it to an empty string isn't going to work...



来源:https://stackoverflow.com/questions/8727858/disable-cache-control-header-field-in-custom-asp-net-httphandler-response

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