在自定义ASP.NET HttpHandler响应中禁用Cache-Control Header字段 [英] Disable Cache-Control Header field in custom ASP.NET HttpHandler response

查看:145
本文介绍了在自定义ASP.NET HttpHandler响应中禁用Cache-Control Header字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何完全抑制我的自定义HttpHandler在ASP.NET中返回的缓存控制标头的输出?

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

我知道,可以通过修改response.Cache和response.Cache.SetCacheability来更改标头字段,但这只会更改标头字段,而不会将其删除。那不是我想要的。我完全不想对Cache-Control字段做任何假设,而让它由浏览器策略决定。

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.

编辑:HttpResponse.Charset也是这样。如果未设置任何字符集,尽管返回不带charset参数的标头 Content-Type:text / html是完全有效的html,ASP.NET始终将其设置为 utf-8。知道如何也禁止字符集吗?

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?

推荐答案

您可以使用

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

如果您使用的是IIS7集成管道,但是某些标头值受到保护,无法删除(即它们是在最常见的页面生命周期中处理完响应后注入的)。

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).

如果以上操作失败(即受保护),您可以做的是使模块手动挂接到 PreSendRequestHeaders ,然后使用上面的代码删除标头。

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.

我相信,使用自定义http处理程序,您应该能够在处理程序实例化时将其删除。

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

这篇关于在自定义ASP.NET HttpHandler响应中禁用Cache-Control Header字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆