Set ETag for FileResult - MVC 3

蓝咒 提交于 2019-12-05 03:40:27
Pedro

The ETag will be suppressed if you use HttpCacheability.Private.

You can find more information on this question

If you change it to HttpCacheability.ServerAndPrivate it should work

BZ,

try doing this stuff in the ExcecuteResult override as it's too late by the time the ActionResult is being thrown back (an example from a project that i quickly opened, edit to suit):

public override void ExecuteResult(ControllerContext context)
{
    var textWriter = new StringWriter();
    var viewResult = GetViewResult(textWriter);
    viewResult.ExecuteResult(context);

    var result = textWriter.ToString()
        .RegexReplace("<script.*?>", string.Empty)
        .Replace("</script>", string.Empty);

    //#if RELEASE
    //context.HttpContext.Response.CacheControl = "Public";
    //context.HttpContext.Response.Expires = CacheDuration;
    string version = "1.0"; //your dynamic version number
    HttpCachePolicyBase cache = context.HttpContext.Response.Cache;
    TimeSpan expireTs = TimeSpan.FromDays(CacheDuration);
    cache.SetCacheability(HttpCacheability.Private);
    cache.SetETag(version);
    cache.SetExpires(DateTime.Now.Add(expireTs));
    cache.SetMaxAge(expireTs);
   //#endif

    context.HttpContext.Response.ContentType = "text/javascript";
    context.HttpContext.Response.Write(result);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!