Using outputcache in MVC

☆樱花仙子☆ 提交于 2020-01-16 03:28:08

问题


I have an action declared as following

[Route("{language}/Navigation/Test")]
[OutputCache(Duration = 3600, VaryByParam = "none")]
public ActionResult Test()
{
   return View();
}

In order to check outputcache setting I added @DateTime.Now.Ticks.ToString() in view Test.cstml

What troubles me is that when i run http://localhost/EN/Navigation/Test first time, view gets cached and page refresh returns a same number of ticks. Now if i change language and set http://localhost/DE/Navigation/Test number of tick changes, ie. view is not served from cache.

I tried to remove VaryByParam = "none" but is always produces the same results.

What is wrong here, how to serve a cached view not matter what language is used.


回答1:


VaryByParam varies by the parameters passed in a URL. I.e. The URL www.stackoverflow.com/page?param1=5. Because DE is a different URL to EN, the page won't be found in the cache so it requests a new one.

From MSDN

A semicolon-separated list of strings used to vary the output cache. By default, these strings correspond to a query string value sent with GET method attributes, or a parameter sent using the POST method. When this attribute is set to multiple parameters, the output cache contains a different version of the requested document for each specified parameter. Possible values include none, *, and any valid query string or POST parameter name.

Bottom line: It's based on URL, not routing. You are able to configure based on the query string but no more.



来源:https://stackoverflow.com/questions/30219251/using-outputcache-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!