我正在对RazorViewEngine进行子类化,我正在尝试覆盖FindView,我对如何使用ViewLocationCache
实现缓存感到困惑。
有谁能举个例子?
答案 0 :(得分:1)
我终于明白了。 这是我的整个实现:
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
//Implement defualt exceptions
if(controllerContext == null)
throw new ArgumentNullException("The controllerContext parameter is null");
if(string.IsNullOrEmpty(viewName))
throw new ArgumentException("The viewName parameter is null or empty.");
//Check cache if specified
if(useCache && this.ViewLocationCache != null){
string cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, generateCacheKey(controllerContext, viewName));
if (!string.IsNullOrEmpty(cachedLocation))
return new ViewEngineResult(CreateView(controllerContext, cachedLocation, masterName), this);
}
//Create arguments for location formatting
string trimmedViewName = string.Empty;
if (viewName.EndsWith(".cshtml"))
trimmedViewName = viewName.Remove(viewName.Length - 7);
else
trimmedViewName = viewName;
object[] args = new object[] { trimmedViewName, controllerContext.RouteData.GetRequiredString("controller"), controllerContext.RouteData.GetRequiredString("module") };
//Attempt to locate file
List<string> searchedLocations = new List<string>();
foreach(string location in ViewLocationFormats){
string formatedLocation = string.Format(location,args);
searchedLocations.Add(formatedLocation);
if (FileExists(controllerContext, formatedLocation))
{
//File has been found. Add to cache and return view
if(this.ViewLocationCache != null)
ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, generateCacheKey(controllerContext, viewName), formatedLocation);
return new ViewEngineResult(CreateView(controllerContext, formatedLocation, masterName), this);
}
}
//Couldnt find view, return searched locations
return new ViewEngineResult(searchedLocations);
}
public string generateCacheKey(ControllerContext controllerContext, string viewName)
{
return string.Format("{0}|{1}", controllerContext.RouteData.GetRequiredString("module"), viewName);
}