23

I've recently returned to a web site project that has been on the backburner. Since recommencing work I've noticed css and javascript changes aren't being recognised by the application when it's running in Visual Studio Development Server. This previously worked fine. I could change stylesheets and javascript and run the app and test. Now the only way I can test the application is to publish it to my local IIS server (though I believe restarting my pc also fixes the problem).

Using Visual Studio 2008 (9.0.30729.1)

The things that have changed since I am certain it was working correctly is the operating system, from Vista x64 Home Premium Edition to Win 7 x64 Ultimate and I've moved the project source from c: to d:. Though I moved back to C: with the same negative results. I've also moved some functionality out in seperate WCF services, hosted by console apps (two service hosts) which are put into debug when the web site starts up in debug.

Stopping the dev server does nothing. Checking out (using VSS 2005) ALL the files to ensure nothing is read only does nothing. The only thing that works is publishing to IIS or restarting my machine. Both undesirable. I'll even be satisfied if there's a process I need to terminate each time I stop debugging, to ensure not using previous copies of things.

Any solutions?

Eilon
  • 25,103
  • 3
  • 82
  • 100
Rob Gray
  • 3,111
  • 4
  • 31
  • 34
  • Even though there are _manual_ workarounds for this (Ctrl + F5), I still created a Microsoft Connect issue to try and have the IDE Development Environment preform these steps automatically. See it here: https://connect.microsoft.com/VisualStudio/feedback/details/800421/visual-studio-development-server-not-reflecting-updated-css – atconway Sep 12 '13 at 20:10

4 Answers4

33

The server may be sending headers to the browser that cause it to keep using cached copies. The simple way to test this is to empty your browser cache.

If that fixes it, you need to study the HTTP headers you get from the server. The developer tools (a.k.a. F12 tools) in your browser of choice will expose the headers returned by the server. Then decide if you want to keep using these caching settings (good for speed) or change them (good for development).

And how do you adjust these headers, you ask? It depends on the server. Here is a link to the instructions for common servers:

Community
  • 1
  • 1
Warren Young
  • 36,691
  • 8
  • 76
  • 94
  • 1
    Thank you! clearing my browser cache fixed the problem. I wish I tried this three days ago :) – Rob Gray Oct 14 '09 at 00:21
  • 15
    An easier way to do this would be pressing CTRL+F5 in your browser – sshow Oct 14 '09 at 00:27
  • 8
    This isn't working for me. Tested with 4 different browsers. Emptied the cache on all of them. Hit CTRL+F5 more times than I could count. Asp.net development server just doesn't want to update with any markup/css/javascript changes. Stopping and starting the testing server is the only way to make it recognize changes. – NinjaBomb Mar 08 '11 at 16:29
  • Yeah this fixes it for me but when tweaking the CSS this seems a clunky solution. What is the headers I should be looking at? and How do I change them – Chad Mar 02 '17 at 19:39
  • @WarrenYoung - I have the problem being that I am trying to tweak my CSS and have to keep clearing my cache to get the new CSS to load. So how do I change the headers to make it stop doing that – Chad Mar 02 '17 at 22:00
  • @Chad: I've added links to instructions for three popular servers. Questions deeper than that are probably [answered at Server Fault already](http://serverfault.com/search?q=http+cache). – Warren Young Mar 03 '17 at 05:37
9

A quick way is to add random parameters after the script or css file's src attribute. for example

<script type="javascript" src="@Url.Content("~/scripts/myScripts.js?" + DateTime.Now.ToString("ddMMHHmmss")"></script>

so browser will always assume its a new file and will not cache.

Be sure to remove this when deploying on live server.

adeel41
  • 2,739
  • 1
  • 26
  • 23
1

If you always want to get the last version of js and CSS files, you could modify your StaticFile middleware like

app.UseStaticFiles(new StaticFileOptions()
{
    OnPrepareResponse = context =>
    {
        context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
        context.Context.Response.Headers.Add("Expires", "-1");
    }
});

or you could add asp-append-version="true" to your file references like:

<link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/>
Shadow Walker
  • 178
  • 1
  • 11
0

Try to use

<link href"~/Content/Style.css" rel="stylesheet"/>

together with bundles. This worked for me

Wairimu Murigi
  • 2,057
  • 2
  • 12
  • 17