已移除代码中Pragma配置,为何ASP.Net与Angular JS响应头仍存在该字段?
彻底移除ASP.Net + AngularJS站点中的Pragma响应头
ASP.Net后端处理方案
ASP.Net框架或IIS常会默认注入Pragma头,可通过以下几种方式全局清除:
1. Web.config全局配置移除
在system.webServer节点(IIS集成模式)下添加自定义头移除规则:
<system.webServer> <httpProtocol> <customHeaders> <remove name="Pragma" /> </customHeaders> </httpProtocol> </system.webServer>
若使用IIS经典模式,需在system.web节点下配置:
<system.web> <customHeaders> <remove name="Pragma" /> </customHeaders> </system.web>
2. Global.asax代码动态清除
在Application_PreSendRequestHeaders事件中拦截响应,移除Pragma头,确保所有请求都生效:
protected void Application_PreSendRequestHeaders(object sender, EventArgs e) { var response = HttpContext.Current.Response; response.Headers.Remove("Pragma"); }
集成模式下直接调用Remove方法即可,无需额外判断,不存在权限问题。
3. 静态资源针对性处理
如果Pragma头仅出现在JS、CSS、图片等静态资源响应中,是IIS静态内容模块默认添加的,可在staticContent节点中配置:
<staticContent> <remove name="Pragma" /> </staticContent>
也可以直接禁用静态资源缓存,从根源避免相关头生成:
<staticContent> <clientCache cacheControlMode="DisableCache" /> </staticContent>
AngularJS前端排查要点
响应头通常由后端返回,但如果是开发环境下出现,需检查:
- 本地开发服务器(如Gulp/Grunt内置服务器)的中间件配置,是否有自动添加Pragma头的逻辑
- AngularJS的
$http拦截器,确认没有误加响应头的代码,若有可手动删除:
angular.module('yourApp') .config(['$httpProvider', function($httpProvider) { $httpProvider.interceptors.push(function() { return { response: function(response) { delete response.headers()['Pragma']; return response; } }; }); }]);
验证方式
修改配置后,用浏览器DevTools的Network面板查看响应头,或用curl命令测试:
curl -I https://your-site-domain.com/target-page
确认Pragma头已从响应中消失。
内容的提问来源于stack exchange,提问作者Rolando F
相关产品推荐
相关产品推荐

