Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have found the answer after digging a bit in the <a href="http://aspnetwebstack.codeplex.com/SourceControl/latest" rel="noreferrer">MVC 4 source code</a>.</p> <p>The <code>RazorViewEngine</code> derives from <code>BuildManagerViewEngine</code>, and this one in turns derives from <code>VirtualPathProviderViewEngine</code>. It is <code>VirtualPathProviderViewEngine</code> the one that implements the method <code>FindView</code>:</p> <pre><code> public virtual ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (String.IsNullOrEmpty(viewName)) { throw new ArgumentException(MvcResources.Common_NullOrEmpty, "viewName"); } string[] viewLocationsSearched; string[] masterLocationsSearched; string controllerName = controllerContext.RouteData.GetRequiredString("controller"); string viewPath = GetPath(controllerContext, ViewLocationFormats, AreaViewLocationFormats, "ViewLocationFormats", viewName, controllerName, CacheKeyPrefixView, useCache, out viewLocationsSearched); string masterPath = GetPath(controllerContext, MasterLocationFormats, AreaMasterLocationFormats, "MasterLocationFormats", masterName, controllerName, CacheKeyPrefixMaster, useCache, out masterLocationsSearched); if (String.IsNullOrEmpty(viewPath) || (String.IsNullOrEmpty(masterPath) &amp;&amp; !String.IsNullOrEmpty(masterName))) { return new ViewEngineResult(viewLocationsSearched.Union(masterLocationsSearched)); } return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this); } </code></pre> <p>That <code>GetPath</code> method used there will do something like this when the view path has not been cached yet:</p> <pre><code>return nameRepresentsPath ? GetPathFromSpecificName(controllerContext, name, cacheKey, ref searchedLocations) : GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, cacheKey, ref searchedLocations); </code></pre> <p>Getting there! The interesting method is <code>GetPathFromGeneralName</code>, which is the one trying to build the whole path for the view and checking if that path exists. The method is looping through each of the view locations that were registered in the View Engine, updating the view path with the display mode <strong>valid for current HttpContext</strong> and then checking if the resolved path exists. If so, the view has been found, is assigned to the result, cached and the result path returned.</p> <pre><code>private string GetPathFromGeneralName(ControllerContext controllerContext, List&lt;ViewLocation&gt; locations, string name, string controllerName, string areaName, string cacheKey, ref string[] searchedLocations) { string result = String.Empty; searchedLocations = new string[locations.Count]; for (int i = 0; i &lt; locations.Count; i++) { ViewLocation location = locations[i]; string virtualPath = location.Format(name, controllerName, areaName); DisplayInfo virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, controllerContext.HttpContext, path =&gt; FileExists(controllerContext, path), controllerContext.DisplayMode); if (virtualPathDisplayInfo != null) { string resolvedVirtualPath = virtualPathDisplayInfo.FilePath; searchedLocations = _emptyLocations; result = resolvedVirtualPath; ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, virtualPathDisplayInfo.DisplayMode.DisplayModeId), result); if (controllerContext.DisplayMode == null) { controllerContext.DisplayMode = virtualPathDisplayInfo.DisplayMode; } // Populate the cache for all other display modes. We want to cache both file system hits and misses so that we can distinguish // in future requests whether a file's status was evicted from the cache (null value) or if the file doesn't exist (empty string). IEnumerable&lt;IDisplayMode&gt; allDisplayModes = DisplayModeProvider.Modes; foreach (IDisplayMode displayMode in allDisplayModes) { if (displayMode.DisplayModeId != virtualPathDisplayInfo.DisplayMode.DisplayModeId) { DisplayInfo displayInfoToCache = displayMode.GetDisplayInfo(controllerContext.HttpContext, virtualPath, virtualPathExists: path =&gt; FileExists(controllerContext, path)); string cacheValue = String.Empty; if (displayInfoToCache != null &amp;&amp; displayInfoToCache.FilePath != null) { cacheValue = displayInfoToCache.FilePath; } ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId), cacheValue); } } break; } searchedLocations[i] = virtualPath; } return result; } </code></pre> <p>You may have noticed that I haven´t talked about a piece of code with the following comment (reformatted for clarity):</p> <pre><code>// Populate the cache for all other display modes. // We want to cache both file system hits and misses so that we can distinguish // in future requests whether a file's status was evicted from the cache // (null value) or if the file doesn't exist (empty string). </code></pre> <p>That (and the piece of code below the comment :)) means that once MVC 4 has found the first valid path from the View Locations registered in the View Engine, <strong>it will also check if the view file for all of the additional display modes that were not tested exist</strong>, so that information can be included in the cache (although just for that view location and not all of the locations available in the view engine). Notice also, how it is passing a lambda to each of the tested display modes for checking if the file for that mode exists:</p> <pre><code>DisplayInfo displayInfoToCache = displayMode.GetDisplayInfo( controllerContext.HttpContext, virtualPath, virtualPathExists: path =&gt; FileExists(controllerContext, path)); </code></pre> <p>So that explains why when you override <code>FileExists</code> it is also being called for the mobile view, even when it has already found the non-mobile view.</p> <p>In any case, <strong>display modes can be removed</strong> the same way they can be added: by updating the DisplayModes collection when the application starts. For example, removing the Mobile display mode and leaving just the default and unspecific one (You cannot clear the collection or no view will ever be found):</p> <pre><code>... using System.Web.WebPages; ... protected void Application_Start() { DisplayModeProvider.Instance.Modes.Remove( DisplayModeProvider.Instance.Modes .Single(m =&gt; m.DisplayModeId == "Mobile")); </code></pre> <p>Quite a long answer but hopefully it makes sense!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload