asp.net-mvc – ASP.NET MVC Web API缓存控制头部没有发送响应,尽管配置在响应对象上
发布时间:2020-05-24 00:43:33 所属栏目:asp.Net 来源:互联网
导读:我试图在ASP.NET MVC Web API中设置缓存头,但IIS的响应表明CacheControl值被忽略. 我原来的假设是我在System.Web.Http.Cors中使用了EnableCorsAttribute,这在这个用例中是必需的.然而,即使没有该属性,响应Cache-Control头仍然是“私有”. 有没有什么我在这里
|
我试图在ASP.NET MVC Web API中设置缓存头,但IIS的响应表明CacheControl值被忽略. 我原来的假设是我在System.Web.Http.Cors中使用了EnableCorsAttribute,这在这个用例中是必需的.然而,即使没有该属性,响应Cache-Control头仍然是“私有”. 有没有什么我在这里做错了? // GET api/<version>/content
// [EnableCors(origins: "*",headers: "*",methods: "*")]
public HttpResponseMessage Get(HttpRequestMessage request)
{
int cacheMaxAgeSeconds;
string cacheMaxAgeString = request.GetQueryString("cache-max-age") ?? request.GetQueryString("cache-max-age-seconds");
string rawUri = request.RequestUri.ToString();
try
{
cacheMaxAgeSeconds = cacheMaxAgeString == null ? Config.ApiCacheControlMaxSeconds : int.Parse(cacheMaxAgeString);
}
catch (Exception ex)
{
cacheMaxAgeSeconds = Config.ApiCacheControlMaxSeconds;
//...
}
try
{
//...
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("...",Encoding.UTF8,"application/json")
};
response.Headers.CacheControl = new CacheControlHeaderValue
{
Public = true,MaxAge = TimeSpan.FromSeconds(cacheMaxAgeSeconds)
};
return response;
}
catch (Exception apiEx)
{
//...
}
}
响应 HTTP/1.1 200 OK Cache-Control: private Content-Type: application/json; charset=utf-8 Date: Thu,23 Jul 2015 10:53:17 GMT Server: Microsoft-IIS/7.5 Set-Cookie: ASP.NET_SessionId=knjh4pncbrhad30kjykvwxyz; path=/; HttpOnly X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 2367 Connection: keep-alive 解决方法以下代码在vanilla WebApi应用程序(System.Web.Http 4.0.0.0)中正确设置“cache-control:public,max-age = 15”.所以…它可能不是WebApi本身导致的问题.您的项目中可能会有一些魔法改变缓存设置(想到全局动作过滤器或类似的东西).或者也许你正在通过代理重写HTTP头. public HttpResponseMessage Get()
{
var content = new JavaScriptSerializer().Serialize(new { foo = "bar" });
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(content,"application/json")
};
response.Headers.CacheControl = new CacheControlHeaderValue
{
Public = true,MaxAge = TimeSpan.FromSeconds(15)
};
return response;
}
// returns in the response: "Cache-Control: public,max-age=15" (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Asp.net MVC Razor – 自定义javascript里面的if块
- asp.net核心 – 如何在自己的主机环境中的ASP.NET Core中gz
- asp.net-mvc – MVC 5 OWIN登录声明和AntiforgeryToken.我错
- 在ASP.NET应用程序的global.asax中处理Application_Error
- asp.net-mvc – 用于支持多种语言的ASP MVC技术
- asp.net-mvc – EF实体与服务模型与查看模型(MVC)
- asp.net – ActiveDirectoryMembershipProvider – “无法建
- asp.net – 如何为客户端和服务器缓存设置不同的缓存到期时
- asp.net – 为什么Request.IsSecureConnection返回false,如
- asp.net – 如何将http请求转发到https网址
推荐文章
站长推荐
热点阅读
