简体   繁体   中英

How to force updates / prevent HTML caching of .NET Core web application running on Linux and hosted by an nginx reverse proxy server

I wrote my web application in C# using .NET Core 5.0 MVC. I am using routing in the controller to map URLS to and serve Views that use Razor. I run the application on a Linux server using the .Net core runtime and then host the application using an nginx reverse proxy server.

I make frequent changes to my website and it relies on real-time pulls from my database to get the latest stats to show to the user so it is vital that the views I serve are up to date.


PROBLEM: For some reason, I can publish and deploy new code and it'll show up fine on my desktop but not on mobile or my friends desktops (sometimes it does sometimes it doesn't). I can see new changes if I change the URL slightly like google.com/test to google.com/Test. My paths are not case-sensitive either but all of my SEO/google links have the lowercase/cached version.


After researching, I concluded that the different browsers/apps are caching my HTML/JS/CSS and not getting the latest from the server.

I have tried the following :

  • Installing the NuGet Package "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" and calling

     services.AddControllersWithViews().AddRazorRuntimeCompilation();

in my ConfigureServices method.

  • Adding the following to my layout file:

     <meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="expires" content="-1" /> <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> <meta http-equiv="pragma" content="no-cache" />
  • Adding this to my controller actions that return the views:

     [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  • I am hosting my web app server using an nginx reverse proxy server, and I tried adding some of the stuff I saw online to prevent caching with no luck.

Nothing has worked consistently for my Mobile Device (Android running Chrome).

Has anyone else encountered this or possibly know of a solution?

Have you tried to use asp-append-version which is provided by ASP.NET ?

It can be used this way for css/js

        <link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM