Thursday, April 15, 2010

WebFormViewEngine - how the MVC finds "The View"

Simplified pseudocode: if(useCache) return ViewLocationCache.GetViewLocation(cacheKey) if(IsSpecificPath) //"~" or "/" VirtualPathProvider.FileExists(name); //viewName else if(usingAreas) locations=AreaLocationFormats+LocationFormats else locations=LocationFormats GetPathFromGeneralName(name, controllerName, areaName ,locations) string virtualPath = location.Format(name, controllerName, areaName); VirtualPathProvider.FileExists(virtualPath); So it uses name directly as VirualPath, or tries each virtual path in locations (with or without area specific first) and than tries each. If in cahing mode, returns from cache based on cacheKey. Formats: //{0} - name (viewName), not staring with ~ or / //{1} - controllerName, ControllerContext.RouteData.GetRequiredString("controller"); //{2} - areaName , AreaHelpers.GetAreaName(controllerContext.RouteData); WebFormViewEngine: MasterLocationFormats = new[] { "~/Views/{1}/{0}.master", "~/Views/Shared/{0}.master" }; AreaMasterLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.master", "~/Areas/{2}/Views/Shared/{0}.master", }; ViewLocationFormats = new[] { "~/Views/{1}/{0}.aspx", "~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx" }; AreaViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.aspx", "~/Areas/{2}/Views/{1}/{0}.ascx", "~/Areas/{2}/Views/Shared/{0}.aspx", "~/Areas/{2}/Views/Shared/{0}.ascx", }; PartialViewLocationFormats = ViewLocationFormats; AreaPartialViewLocationFormats = AreaViewLocationFormats; // format is ":ViewCacheEntry:{cacheType}:{prefix}:{name}:{controllerName}:{areaName}:" private const string _cacheKeyFormat = ":ViewCacheEntry:{0}:{1}:{2}:{3}:{4}:"; See MVC source code. P.S. It would be nice to append this inside some poster ;-) Any better explanation links are welcomed.

No comments:

Post a Comment