Prevent scripts from being cached programmatically

后端 未结 6 1462
面向向阳花
面向向阳花 2021-01-23 16:23

I would like to ask if there is a way to prevent Firefox from caching scripts (.js files).

I have a project (ASP.Net Web App) with caching issue on firefox. When I first

6条回答
  •  醉酒成梦
    2021-01-23 17:08

    You should be able to use

    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    but you will have to have a http handler that renders the script directly to the response stream and than set the source url of the script tag to the handler or configure IIS to handle all javascripts using a specific handler: Point *.js to the aspnet_isapi.dll ISAPI Extension and than add the following in your web.config

    
        
            
            
        
    

    And than the handler:

    namespace skmHttpHandlers
    {
       public class JavascriptHandler : IHttpHandler
       {
          public void ProcessRequest(HttpContext context)
          {
             context.Response.Clear();
             context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
             using (var scriptStream = File.Open(Server.MapPath("~/script/TheScript.js"), FileMode.Open))
               scriptStream.CopyTo(context.Response.OutputStream);
             context.Response.End();
          }
    
          public bool IsReusable
          {
             get
             {
                return false;
             }
          }
       }
    }
    

提交回复
热议问题