Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is ASP.NET View Engine checking for .Mobile.chstml view path?
    primarykey
    data
    text
    <p>For my ASP.NET MVC 4 project, I'm trying to implement a custom view engine to find an "Index.cshtml" view file if one exists within a folder. Additionally, I'm throwing a 404 for all view paths that are not found.</p> <p>The 404 works when a view file doesn't exist. When a view file does exist, the view engine will then try looking for a .Mobile.cshtml file using the FileExists() function. There is no .mobile.cshtml file, so it throws an exception. Why does the view engine still look for a .mobile.cshtml file when it has found the non-mobile file already? </p> <p>For example, when the view engine is able to find a view path at "~/Views/About/History/Index.cshtml", it will then try finding the file "~/Views/About/History/Index.Mobile.cshtml". Below is my full code for the custom view engine.</p> <pre><code>namespace System.Web.Mvc { // Extend where RazorViewEngine looks for view files. // This looks for path/index.ext file if no path.ext file is found // Ex: looks for "about/history/index.chstml" if "about/history.cshtml" is not found. public class CustomViewEngine : RazorViewEngine { public BeckmanViewEngine() { AreaViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}/Index.cshtml", }; ViewLocationFormats = new[] { "~/Views/{1}/{0}/Index.cshtml", }; } // Return 404 Exception if viewpath file in existing path is not found protected override bool FileExists(ControllerContext context, string path) { if (!base.FileExists(context, path)) { throw new HttpException(404, "HTTP/1.1 404 Not Found"); } return true; } } } </code></pre>
    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.
 

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