c# - 如何实现RazorViewEngine FindView缓存

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

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

谁能举个例子?

最佳答案

我终于明白了。 这是我的整个实现:

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);
    }

关于c# - 如何实现RazorViewEngine FindView缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13675784/

相关文章:

c# - 如何仅比较 LINQ to Entities 中 TimeSpan 的分钟部分?

用于生成唯一数字的 C# 按位操作

c# - .NET Extension Objects with XSLT——如何遍历一个集合?

c# - 尝试连接时,Azure Vault 中的 GetSecret 抛出 NullReferenceException

c# - dotnet-run 命令未按预期工作

c# - 了解枚举

.net - 将.net 2.0解决方案转换为.net 3.5的陷阱

c# - NSubstitute:能够在没有返回类型的模拟方法中设置引用对象

c# - 将 DateTime 与触发代码进行比较的最佳方法

asp.net - 通过 https 有选择地缓存 .js 和 .png 文件?