[英]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.我使用 .NET Core 5.0 MVC 用 C# 编写了我的 Web 应用程序。 I am using routing in the controller to map URLS to and serve Views that use Razor.
我在控制器中使用路由来将 URLS 映射到使用 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.
我使用 .Net 核心运行时在 Linux 服务器上运行该应用程序,然后使用 nginx 反向代理服务器托管该应用程序。
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.
如果我将 google.com/test 之类的 URL 稍微更改为 google.com/Test,我可以看到新的变化。 My paths are not case-sensitive either but all of my SEO/google links have the lowercase/cached version.
我的路径也不区分大小写,但我所有的 SEO/google 链接都有小写/缓存版本。
After researching, I concluded that the different browsers/apps are caching my HTML/JS/CSS and not getting the latest from the server.经过研究,我得出结论,不同的浏览器/应用程序正在缓存我的 HTML/JS/CSS,而不是从服务器获取最新的。
I have tried the following :我尝试了以下方法:
Installing the NuGet Package "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" and calling安装 NuGet 包“Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation”并调用
services.AddControllersWithViews().AddRazorRuntimeCompilation();
in my ConfigureServices method.在我的 ConfigureServices 方法中。
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.我正在使用 nginx 反向代理服务器托管我的 Web 应用程序服务器,并且我尝试添加一些我在网上看到的内容以防止没有运气的缓存。
Nothing has worked consistently for my Mobile Device (Android running Chrome).我的移动设备(运行 Chrome 的 Android)没有任何效果。
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 ?您是否尝试过使用 ASP.NET 提供的asp-append-version ?
It can be used this way for css/js它可以以这种方式用于 css/js
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.