How do I clear the view file location cache used by the ViewEngine in ASP.Net MVC 3
The ViewEngine in ASP.Net MVC 3 caches the physical paths of the views and partial views which is causing errors when I add or move view files in my production environment. Is there a way I can clear that cache at runtime? I found one article online that says that cache is开发者_JAVA百科 stored in HttpContext.Cache, but I'm not sure which entry it is.
Here's the key used by the Razor view engine:
// System.Web.Mvc.VirtualPathProviderViewEngine
private string CreateCacheKey(string prefix, string name, string controllerName, string areaName)
{
return string.Format(CultureInfo.InvariantCulture, ":ViewCacheEntry:{0}:{1}:{2}:{3}:{4}:", new object[]
{
base.GetType().AssemblyQualifiedName,
prefix,
name,
controllerName,
areaName
});
}
So for example if you wanted to clear the cache location of the Index
view for Home
controller you would remove the following key from the HttpContext.Cache
:
HttpContext.Cache.Remove(":ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:View:Index:Home::");
and for the _LogOnPartial.cshtml
partial:
HttpContext.Cache.Remove(":ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:Partial:_LogOnPartial:Home::");
You should obviously be aware that you are using a totally undocumented feature that could be changed without any notice and your code could stop working in a future version of ASP.NET MVC.
精彩评论