How to change default view location scheme in ASP.NET MVC?

A simple solution would be to, in your Appication_Start get hold of the appropriate ViewEngine from the ViewEngines.Engines collection and update its ViewLocationFormats array and PartialViewLocationFormats. No hackery: it’s read/write by default.

protected void Application_Start()
{
    ...
    // Allow looking up views in ~/Features/ directory
    var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
    razorEngine.ViewLocationFormats = razorEngine.ViewLocationFormats.Concat(new string[] 
    { 
        "~/Features/{1}/{0}.cshtml"
    }).ToArray();
    ...
    // also: razorEngine.PartialViewLocationFormats if required
}

The default one for Razor looks like this:

ViewLocationFormats = new string[]
{
    "~/Views/{1}/{0}.cshtml",
    "~/Views/{1}/{0}.vbhtml",
    "~/Views/Shared/{0}.cshtml",
    "~/Views/Shared/{0}.vbhtml"
};

Note that you may want to update PartialViewLocationFormats also.

Leave a Comment