Browser Caching in ASP.NET application

后端 未结 6 2033
暖寄归人
暖寄归人 2020-12-14 21:43

Any suggestions on how to do browser caching within a asp.net application. I\'ve found some different methods online but wasn\'t sure what would be the best. Specifically, I

6条回答
  •  清歌不尽
    2020-12-14 22:08

    Unless you configure IIS to give asp.net control of js/css/image requests it won't see them by default, hence your best plan (for long term maintainability) is to deliberately tweak the response headers at your firewall/trafficmanager/server or (better and what most of the world does at this point) to version your files in path, i.e:

    Instead of writing this in your mark-up:

    http://www.foo.com/cachingmakesmesad.css
    

    Use this:

    http://www.foo.com/cachingmakesmesad.css?v1
    

    ..and change the version number when you need to effectively clear cache. If that's every time then you could even append a GUID or datestamp instead, but I can't think of any occasion where I would want to do that really.


    I thought your question was anti-cache but re-reading it I see I wasted a good answer there :P

    Long story short, browsers are normally very aggressively pro-caching "simple" resources so you shouldn't have to worry about this, but if you did want to do something about it you would have to have access to a firewall/trafficmanager/IIS for the reasons above (ASP.NET won't be given a chance by default).

    However... there is no way you can absolutely force caching, and nor should you. What is and isn't cached is rightfully the decision of the end-user, all you can do is strongly request.

提交回复
热议问题