【问题标题】:How to implement RazorViewEngine FindView Caching如何实现 RazorViewEngine FindView 缓存
【发布时间】:2012-12-03 01:04:01
【问题描述】:

我正在继承 RazorViewEngine,我正在尝试覆盖 FindView,但我对你如何使用 ViewLocationCache 实现缓存感到困惑。

谁能举个例子?

【问题讨论】:

    标签: c# .net asp.net-mvc asp.net-mvc-3


    【解决方案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);
        }
    

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2010-11-11
      相关资源
      最近更新 更多