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.

November 29th, 2015 • MVC •
0 (0 votes)
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.


Was this informative? Share it!

Looking to become a better developer?

Sign up to receive ReSharper Design Pattern Smart Templates, ASP.NET MVC Guidelines Checklist, and Newsletter Updates!

Picture of Jonathan Danylko

Jonathan Danylko is a freelance web architect and avid programmer who has been programming for over 20 years. He has developed various systems in numerous industries including e-commerce, biotechnology, real estate, health, insurance, and utility companies.

When asked what he likes to do in his spare time, he replies, "Programming."

comments powered by Disqus