简体   繁体   English

如何强制更新/防止在 Linux 上运行并由 nginx 反向代理服务器托管的 .NET Core Web 应用程序的 HTML 缓存

[英]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.

相关问题 如何启用 nginx 反向代理以与 gRPC in.Net core 一起使用? - How to enable nginx reverse proxy to work with gRPC in .Net core? How to set BaseUrl for Swagger in ASP.NET Core Web API using NSwag.AspNetCore (when hosted behind a reverse proxy) - How to set BaseUrl for Swagger in ASP.NET Core Web API using NSwag.AspNetCore (when hosted behind a reverse proxy) 如何使用集群内反向代理自动确定在Kubernetes中运行的ASP.NET Core应用程序的KnownNetworks? - How to automatically determine KnownNetworks for ASP.NET Core application running in Kubernetes with an in-cluster reverse proxy? 具有Apache和反向代理的Linux ASP.Net核心 - Linux ASP.Net core with Apache and reverse proxy 如何从运行在 Linux 上的 .NET 核心应用程序关闭计算机 - How to shutdown computer from a .NET Core application running on Linux https在Z1848E7322214CCE460F9B24F9B24F5E72Z CORE 3.1 Z2567A.AS SERSER 3.1 - force https on an asp.net core 3.1 web app running as a service on a windows server 2008 rs server 如何在自托管的.net核心mvc应用程序中运行长时间运行或重复运行的任务? - How should I run long-running or recurring taks in a self-hosted .net core mvc application? ASP.NET 作为 Linux 服务运行的核心 web 应用程序未正确停止 - ASP.NET Core web application running as a Linux service does not stop properly 你会在 .NET Core web/api 应用程序中的什么地方实现缓存? - Where would you implement caching in a .NET Core web/api application? 运行 STA 线程的 .Net Core Web 应用程序 - .Net Core web application running STA thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM