Quick Tip: Changing the Location of Views

For those looking to point their views to a different directory, here is a quick tip on how you can change the directory of where your views are stored.

Written by Jonathan "JD" Danylko • Last Updated: • MVC •
Different Locations of Views

Ok, quick question.

How many of you knew there was a ViewEngine under the hood that would have the ability to render full and partial Views, tell the Razor engine where those Views were located, and have the ability to include multiple ViewEngines?

I didn't realize this until I started building my own CMS and talked to a number of other developers about how easy it was to swap out a new ViewEngine.

With this ViewEngine abstracted out from MVC, it gave developers free reign to do whatever they wanted with the ViewEngine. It even spawned a number of other ViewEngines, like Spark and NHaml.

By default, ASP.NET MVC includes a WebFormsViewEngine and a RazorViewEngine. Of course, all of the cool kids are using the RazorViewEngine. ;-)

When you start up a brand new project, it automatically includes both the WebFormsViewEngine and the RazorViewEngine. If you don't want to use the WebFormsViewEngine, clear it out (in bold).

Global.asax.cs

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new RazorViewEngine());
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

But did you know that you can tell each ViewEngine where your Views are located?

Here are the locations you can modify:

  • PartialViewLocationFormats - An array of strings providing a hint as to where your Partial Views are stored.
  • ViewLocationFormatsAn array of strings providing a hint as to where your Views are stored.
  • MasterLocationFormats - An array of strings providing a hint as to where your Master pages are stored.

If you don't specify an array of paths, it will use the defaults.

So now you can move your View folders around and structure them just the way you want them in your application.

However, keep in mind that a newbie coming on board may be accustomed to the default standard and moving Views around on a newbie could cause some problems later.

So now your new Global.asax.cs will look like this:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    // ViewEngines.Engines.Clear();
    // ViewEngines.Engines.Add(new RazorViewEngine());
    ViewEngines.Engines.Clear();
    var customEngine = new RazorViewEngine();
    customEngine.PartialViewLocationFormats = new string[]
    {
        "~/Views/{1}/{0}.cshtml",
        "~/Views/Shared/{0}.cshtml",
        "~/Views/PartialViews/{0}.cshtml",
        "~/Views/PartialViews/{1}/{0}.cshtml"
    };
    customEngine.ViewLocationFormats = new string[]
    {
        "~/Views/{1}/{0}.cshtml",
        "~/Views/Shared/{0}.cshtml",
        "~/Views/Controller/{1}/{0}.cshtml"
    };
    customEngine.MasterLocationFormats = new string[]
    {
        "~/Views/Shared/{0}.cshtml",
        "~/Views/Layout/{0}.cshtml"
    };
    ViewEngines.Engines.Add(customEngine);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

This helps out your application when it's trying to find the corresponding Views.

One last thing: You'll notice that the formats use the String.Format syntax based on the {0} and {1}. The first parameter ({0}) is the name of the file and the second parameter ({1}) is the controller name.

Conclusion

Today was just a quick tip on modifying the Razor View Engine to point to specific folders based on how you wanted your application Views structured.

Post some comments below if I missed something or did something wrong. Talk to ya soon.


Did you like this content? Show your support by buying me a coffee.

Buy me a coffee  Buy me a coffee
Picture of Jonathan "JD" Danylko

Jonathan Danylko is a web architect and entrepreneur who's been programming for over 25 years. He's developed websites for small, medium, and Fortune 500 companies since 1996.

He currently works at Insight Enterprises as an Principal Software Engineer Architect.

When asked what he likes to do in his spare time, he replies, "I like to write and I like to code. I also like to write about code."

comments powered by Disqus