示例#1
0
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            string controllerName = controllerContext.RouteData.GetRequiredString("controller");
            object area;
            string viewArea = null;

            if (controllerContext.RouteData.DataTokens.TryGetValue("area", out area))
            {
                viewArea = area as string;
            }
            var loc = ViewLocationFormats
                      .Select(x => String.Format(x, viewName, controllerName, viewArea))
                      .FirstOrDefault(s => File.Exists(MockVirtualPathFactory.NormalizePath(s)));
            var masterLoc = MasterLocationFormats
                            .Select(x => String.Format(x, viewName, masterName, viewArea))
                            .FirstOrDefault(s => File.Exists(MockVirtualPathFactory.NormalizePath(s)));

            if (loc == null)
            {
                return(new ViewEngineResult(ViewLocationFormats));
            }
            else
            {
                var view = new MockView(MockVirtualPathFactory.NormalizePath(loc), loc, true, FileExtensions);
                return(new ViewEngineResult(view, this));
            }
        }
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName,
                                                  string masterName, bool useCache)
        {
            viewName = viewName.ToTitleCaseString();

            if (controllerContext == null)
            {
                throw new ArgumentNullException(nameof(controllerContext), "The controllerContext parameter is null");
            }

            if (string.IsNullOrEmpty(viewName))
            {
                throw new ArgumentException("The viewName parameter is null or empty.", nameof(viewName));
            }

            if (controllerContext.Controller == null)
            {
                return(base.FindView(controllerContext, viewName, masterName, useCache));
            }

            var pluginName = controllerContext.Controller.ToControllerShortName();

            if (string.IsNullOrEmpty(pluginName))
            {
                return(base.FindView(controllerContext, viewName, masterName, useCache));
            }
            var cacheKey = $"{pluginName}|{viewName}";

            if (useCache && ViewLocationCache != null)
            {
                var cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey);
                if (!string.IsNullOrEmpty(cachedLocation))
                {
                    return(new ViewEngineResult(CreateView(controllerContext, cachedLocation, masterName),
                                                this));
                }
            }

            string trimmedViewName;

            if (viewName.EndsWith(".cshtml") || viewName.EndsWith(".vbhtml"))
            {
                trimmedViewName = viewName.Remove(viewName.Length - 7);
            }
            else
            {
                trimmedViewName = viewName;
            }
            var args = new object[] { trimmedViewName, pluginName };

            foreach (var location in ViewLocationFormats)
            {
                var path = string.Format(location, args);

                if (!FileExists(controllerContext, path))
                {
                    continue;
                }

                ViewLocationCache?.InsertViewLocation(controllerContext.HttpContext, cacheKey, path);

                return(new ViewEngineResult(CreateView(controllerContext, path, masterName), this));
            }
            return(new ViewEngineResult(ViewLocationFormats.Select(i => string.Format(i, args))));
        }