Asp.net

ASP.NET MVC 如何禁用自動記憶體選項?

  • October 18, 2012

如何從 asp.Net mvc 應用程序禁用自動瀏覽器記憶體?

因為我在記憶體所有連結時遇到了問題。但有時它會自動重定向到預設索引頁面,該頁面將其儲存在記憶體中,然後在我一直點擊該連結時,它會將我重定向到預設索引頁面。

那麼有人知道如何從 ASP.NET MVC 4 手動禁用記憶體選項嗎?

您可以使用OutputCacheAttribute來控制伺服器和/或瀏覽器對特定操作或控制器中所有操作的記憶體。

禁用控制器中的所有操作

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration
public class MyController : Controller
{
 // ... 
}

禁用特定操作:

public class MyController : Controller
{
   [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will disable caching for Index only
   public ActionResult Index()
   {
      return View();
   }
} 

如果您想將預設記憶體策略應用於所有控制器中的所有操作,您可以通過編輯並查找方法來添加全域操作過濾器。此方法添加在預設的 MVC 應用程序項目模板中。global.asax.cs``RegisterGlobalFilters

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
   filters.Add(new OutputCacheAttribute
                   {
                       VaryByParam = "*",
                       Duration = 0,
                       NoStore = true,
                   });
   // the rest of your global filters here
}

這將導致它將上述OutputCacheAttribute指定應用於每個操作,這將禁用伺服器和瀏覽器記憶體。您仍然應該能夠通過添加OutputCacheAttribute特定操作和控制器來覆蓋此無記憶體。

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