ASP.NET MVC:OutputCache的问题
发布时间:2020-12-15 23:44:51 所属栏目:asp.Net 来源:网络整理
导读:对于我目前的项目,有必要生成动态CSS … 所以,我有一个部分视图作为一个CSS提供者…控制器代码看起来像这样: [OutputCache(CacheProfile = "DetailsCSS")] public ActionResult DetailsCSS(string version,string id) { // Do something with the version a
对于我目前的项目,有必要生成动态CSS …
所以,我有一个部分视图作为一个CSS提供者…控制器代码看起来像这样: [OutputCache(CacheProfile = "DetailsCSS")] public ActionResult DetailsCSS(string version,string id) { // Do something with the version and id here.... bla bla Response.ContentType = "text/css"; return PartialView("_css"); } 输出缓存配置文件如下所示: <add name="DetailsCSS" duration="360" varyByParam="*" location="Server" varyByContentEncoding="none" varyByHeader="none" /> 问题是:当我使用OutputCache行([OutputCache(CacheProfile =“DetailsCSS”)])时,响应是内容类型“text / html”,而不是“text / css”…当我删除它,它按预期工作 所以,对我来说似乎OutputCache在这里没有保存我的“ContentType”设置…有没有办法呢? 谢谢 解决方法
您可以使用自己的ActionFilter覆盖ContentType,该缓冲区在缓存发生后执行.
public class CustomContentTypeAttribute : ActionFilterAttribute { public string ContentType { get; set; } public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.ContentType = ContentType; } } 然后在OutputCache之后调用该属性. [CustomContentType(ContentType = "text/css",Order = 2)] [OutputCache(CacheProfile = "DetailsCSS")] public ActionResult DetailsCSS(string version,string id) { // Do something with the version and id here.... bla bla return PartialView("_css"); } 或者(我还没有尝试过这个),但是用CSS特定的实现来覆盖“OutputCacheAttribute”类.这样的东西 public class CSSOutputCache : OutputCacheAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { base.OnResultExecuting(filterContext); filterContext.HttpContext.Response.ContentType = "text/css"; } } 和这个… [CSSOutputCache(CacheProfile = "DetailsCSS")] public ActionResult DetailsCSS(string version,string id) { // Do something with the version and id here.... bla bla return PartialView("_css"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET – 如何在C#表上设置Cells-Width-Percentage
- asp.net – Chrome浏览器不显示HTTP处理程序生成的图像
- 从ASP.Net页面运行批处理文件
- asp.net-mvc-3 – 为什么asp.net mvc模型绑定器生成system.
- asp.net-mvc – 使用disabled =“disabled”属性创建一个Se
- asp.net-mvc – EF,ASP MVC依赖注入.多个并发请求和数据库连
- asp.net – C#:GDI:使用位图的保存方法过度写入图像
- asp.net中的泛型处理程序是什么?
- 如何在ASP.NET WebService调用中动态初始化文化?
- 处理ASP.Net MVC中的过期标题
推荐文章
站长推荐
热点阅读