Asp.net-Mvc

響應記憶體在 asp.net 核心項目中不起作用

  • June 8, 2022

我嘗試在 asp.net 核心項目中實現響應記憶體,但它不起作用。這是startup.cs

public void ConfigureServices(IServiceCollection services) 
{
 services.AddResponseCaching();
 services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{
   app.UseResponseCaching();

   if (env.IsDevelopment()) 
   {
     app.UseDeveloperExceptionPage();
   }

這是我的控制器。

[ResponseCache(Duration = 30)]
public IActionResult Index() 
{
 ViewBag.IsMobile = RequestExtensions.IsMobileBrowser(ContextAccessor.HttpContext.Request);
 return View();
}

但仍然記憶體控制標頭id

cache-control →no-cache, no-store

我缺乏的地方請幫忙。此響應記憶體不起作用,我從 Microsoft 文件中獲取指導。

請注意,要使記憶體正常工作,必須滿足許多要求:

該請求必須導致來自伺服器的 200(OK)響應。

請求方法必須是 GET 或 HEAD。

終端中間件,例如靜態文件中間件,不得在響應記憶體中間件之前處理響應。

授權標頭不得存在。

Cache-Control 標頭參數必須有效,並且響應必須標記為公共而不標記為私有。

如果 Cache-Control 標頭不存在,則 Pragma: no-cache 標頭/值不得存在,因為 Cache-Control 標頭在存在時會覆蓋 Pragma 標頭。Set-Cookie 標頭不得存在。

Vary 頭參數必須有效且不等於 *.

Content-Length 標頭值(如果已設置)必須與響應正文的大小匹配。

請參閱此網站:aspnet-core-response-cache

還要注意,如果 Postman 發出請求,請禁用“不發送記憶體標頭”選項。

請參閱此文章(最終文章的圖像): ASP.Net Core 2.0 - ResponseCaching Middleware - Not Caching on Server

引用自:https://stackoverflow.com/questions/53941473