asp.net-mvc – MVC捆绑客户端缓存
发布时间:2020-05-27 20:53:15 所属栏目:asp.Net 来源:互联网
导读:默认情况下,一个MVC包在客户端上缓存1年。是否可以手动设置它的客户端头(对于1特定的包)? 我需要的是为我的一个包设置自定义过期标头。我不能依赖“v = hash”查询字符串,因为这个包是一个外部网站,并且他们不会改变指向我的包的URL每次我更改它。 我试过
|
默认情况下,一个MVC包在客户端上缓存1年。是否可以手动设置它的客户端头(对于1特定的包)? 我需要的是为我的一个包设置自定义过期标头。我不能依赖“v = hash”查询字符串,因为这个包是一个外部网站,并且他们不会改变指向我的包的URL每次我更改它。 我试过的是创建一个自定义Bundle类(继承Bundle)和覆盖GenerateBundleResponse()方法。这样我可以控制服务器缓存,但定制客户端缓存的唯一方法是设置BundleResponse.Cacheability(public,private,nocache等)。但我不能手动设置标头。我可以访问BundleContext(和它的HttpContext),但是当我设置头文件上下文,它将影响所有其他请求以及。 解决方法不幸的是没有办法。你可以在捆绑的内部实现中找到原因。在BundleHandler类中,ProcessRequest调用ProcessRequest,Bundle类的内部方法,它在HttpContext.Response.Write之前调用SetHeaders。因此,客户端缓存在响应写入之前设置为一年。注意:BundleHandler是一个内部密封类:internal sealed class BundleHandler:IHttpHandler 在BundleHandler类中: public void ProcessRequest(HttpContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
context.Response.Clear();
BundleContext context2 = new BundleContext(new HttpContextWrapper(context),BundleTable.Bundles,this.BundleVirtualPath);
if (!Bundle.GetInstrumentationMode(context2.HttpContext) && !string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"]))
{
context.Response.StatusCode = 304;
}
else
{
this.RequestBundle.ProcessRequest(context2);
}
}
在Bundle类中: internal void ProcessRequest(BundleContext context)
{
context.EnableInstrumentation = GetInstrumentationMode(context.HttpContext);
BundleResponse bundleResponse = this.GetBundleResponse(context);
SetHeaders(bundleResponse,context);
context.HttpContext.Response.Write(bundleResponse.Content);
}
private static void SetHeaders(BundleResponse bundle,BundleContext context)
{
if (bundle.ContentType != null)
{
context.HttpContext.Response.ContentType = bundle.ContentType;
}
if (!context.EnableInstrumentation)
{
HttpCachePolicyBase cache = context.HttpContext.Response.Cache;
cache.SetCacheability(bundle.Cacheability);
cache.SetOmitVaryStar(true);
cache.SetExpires(DateTime.Now.AddYears(1));
cache.SetValidUntilExpires(true);
cache.SetLastModified(DateTime.Now);
cache.VaryByHeaders["User-Agent"] = true;
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp-classic – ASP/VBScript – Int()vs CInt()
- 在ASP.net中复制PHPBB密码哈希c#
- asp.net核心 – ASP.NET 5,DNXKestrel:没有打破点
- asp.net – 处理缓存和浏览器后退按钮的最佳方法是什么?
- asp.net-mvc – 什么时候应该在ASP.NET MVC中使用异步控制器
- 调试 – 使用ASP.Net Core在VSCode中调试Typescript
- asp.net-mvc – 首先通过NuGet尝试StructureMap和MVC3
- asp.net-mvc – ASP.NET MVC是否有分页解决方案,在数据库中
- asp.net-mvc-3 – 使用AutoMapper携带元数据查看模型的技术
- asp.net – 如何在DLL中添加Web服务引用
推荐文章
站长推荐
- asp.net-mvc – 如何使用ViewBag创建一个下拉列表
- asp.net – 类型存在于’A’和’B’
- asp.net – 通过javascript从代码后面访问变量
- ASP.net vNext和Entity Framework 6
- ASP.net处理XML数据实例浅析
- asp.net – Visual Studio 2015 Web应用程序.NET
- asp.net – 如何在.net中读取Elastic Beanstalk环
- asp.net-mvc-3 – MVC 3 $.ajax – 响应似乎是从
- asp.net-mvc-4 – 没有为此对象定义的无参数构造
- asp.net-mvc – 使用Ninject注册到自定义成员资格
热点阅读
