我在这里有点麻烦。
我有一个操作,其输出相当静态,直到使用另一个操作来更新第一个操作的数据源。我使用 HttpResponse.RemoveOutputCacheItem 删除该操作的缓存输出,以便在用户下次加载它时刷新它。
基本上我有这样的操作:
[OutputCache(Duration=86400, Location=OutputCacheLocation.Server)]
public ActionResult Index()
{
return ...
}
在我的 HomeController 上,以及另一个 Controller 上的另一个操作,用于更新前者中使用的信息:
public ActionResult SaveMenu(int id, Menu menu)
{
...
HttpResponse.RemoveOutputCacheItem(Url.Action("Index", "Home"));
...
}
令人疯狂的是,只要您加载 URL http://site/
或 http://site/Home/Index
,这种方法就有效。 >。当您使用 URL http://site
时,它永远不会刷新。
这是为什么?
最佳答案
这与 OutputCacheAttribute
的工作方式有关,特别是它对 RouteData
不为空的依赖关系。相关部分是:
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (!filterContext.IsChildAction)
{
new OutputCachedPage(this._cacheSettings).ProcessRequest(HttpContext.Current);
}
}
ResultExecutingContext filterContext
派生自 ControllerContext
。这是 ControllerContext.IsChildAction
的来源:
public virtual bool IsChildAction
{
get
{
RouteData routeData = this.RouteData;
if (routeData == null)
{
return false;
}
return routeData.DataTokens.ContainsKey("ParentActionViewContext");
}
}
那么,为什么这与您的问题相关?
因为当您省略“/”时,您的Route
将不匹配任何内容。默认路由是“/
”。这里有一篇文章更深入地解释了这一点:http://www.58bits.com/blog/2008/09/29/ASPNet-MVC-And-Routing-Defaultaspx.aspx 。编写它是为了解释为什么 Default.aspx
文件在 ASP.NET MVC 1 项目中是必需的,但原因是根源于同一个地方。
因此,基本上,RouteData 为 null,因此 OutputCacheAttribute
无法工作。您可以按照 Michael Jasper 的建议并利用 URL 重写来解决您的问题。
关于asp.net-mvc - 主机名中没有尾部斜杠,HttpResponse.RemoveOutputCacheItem 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5051159/