Refresh browser cache automatically

2019-02-14 07:59发布

问题:

This is something I've never had to deal with before so excuse me if I sound ignorant.

The basic issue I'm having is that while working within Visual Studio 2010 any changes I make seem to be caught in some sort of cache.

For example, I'll make a change to a page, run the solution, and then I have to press Ctrl + F5 to force refresh of cache to see the changes. This is with ASP.NET/HTML/CSS & JavaScript code. Same thing.

In the past Visual Studio used to just clear the cache every time I ran, so I'm a little frustrated to say the least.

Additionally, when I deploy the solution to a IIS server, the same thing happens for users. Even though a ASP.NET page had changes on it, the users keep pulling up a cached version until they clear their cache. I've mitigated this issue to a degree by renaming the files (especially javascript) with version numbers so the client always sees them as a new file and loads instead of loading from cache.

Is there any settings within Visual Studio or web.config that could be causing this?

EDIT: Here's my web.config file

<configuration>
  <system.web>
    <sessionState cookieless="UseCookies" timeout="1440" mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424"/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxx"/>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxx"/>
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxx"/>
      </assemblies>
    </compilation>
    <authentication mode="Windows"/>
    <pages>
      <controls>
        <add tagPrefix="ajaxtoolkit" namespace="AjaxControlToolkit" assembly="AjaxControlToolKit"/>
      </controls>
    </pages>
  </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

回答1:

Web resources (Css, Js, Images and even Html) take time to download over the network, which increases the time it takes to load a web page (Steve Souders suggests it takes nearly 80 %, see here) . HTTP caching allows these resources to be saved, or cached, by a browser or proxy. Once a resource is cached, a browser or proxy can refer to the locally cached copy instead of having to download it again on subsequent visits to the web page.

Browser cache can be controlled by HTTP Cache Headers (see a quick overview here)

So, it's not because of Visual Studio or asp.net, but because of the browser.

Hopefully, there are many ways to disable cache at server-side (just for testing purpose) or on client side (to relaod and bypass the cache).

Here are some possibilities :

Using asp.net

You can explicit disable browser cache with this code

// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

// Stop Caching in Firefox
Response.Cache.SetNoStore();

Warning : Do this only on local for testing purpose !

Also, Many bundling systems allow you to control cache settings. asp.net mvc Bundles set the HTTP Expires Header one year from when the bundle is created and add an additionnal parameter in the query string. As long as the bundle doesn't change, this unique identifier will be the same. See here.

On browser

There are many shortcuts to reload a page and bypass the cache: For exampel :

  • Chrome : Shift + Reload Button
  • IE : Ctrl + F5 or Ctrl + Reload Button
  • Firefox : Ctrl + Shift + R or Shift + Reload

Also there are many ways to completely disable cache on browser.

  • IE : Tools ‣ Internet Options ‣ Temporary Internet files ‣ Settings ‣ Check for a new version of stored pages
  • Chrome : Tools ‣ Developer Tools ‣ cogwheel icon in lower left ‣ corner Disable Cache
  • Firefox : Tools ‣ Options ‣ Advanced ‣ Network ‣ cache size =0

Wikipedia has a great list .