[RESOLVED]ViewEngine.MasterLocationFormats

Hi There,

what does the below lines of code do in the Global.asax page in ASP.NET MVC application.

viewEngine.MasterLocationFormats = new string[] { "~/Views/Shared/{0}.cshtml" };

viewEngine.PartialViewLocationFormats = new string[] { "~/Views/Forms/{1}/{0}.cshtml", "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };

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

ViewEngines.Engines.Add(viewEngine);

If i assume that these lines of code go and fetch the master layout formats from the master layouts, I have 10′s of Master layout pages in the folders that are define in here.

please answe

sukumarchalla

viewEngine.MasterLocationFormats = new string[] { "~/Views/Shared/{0}.cshtml" };

This specifies your Master page locations, where to search for.

~/Views/Shared/{0}.cshtml say to probe for the locations of master pages within global View/Shared folder.

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

in _ViewStart.cshtml hooks up your master page

If we are going to add additional master pages using 

viewEngine.MasterLocationFormats = new string[] { "~/Views/Shared/{0}.cshtml", "~/Views/Master/{0}.cshtml" };

This will pick up the first match. More like probing for Master files in specified locations. 

If you need to change master pages dynamically try

http://weblogs.asp.net/imranbaloch/view-engine-with-dynamic-view-location (seems nice)

http://stackoverflow.com/questions/2899260/how-to-dynamically-change-masterpage-in-asp-net-mvc-application

http://codeofrob.com/entries/dynamically-switching-between-master-pages-in-asp.net-mvc.html 

This is helpful.

I have one more question on the same.

what does "{ "~/Views/Forms/{1}/{0}.cshtml", "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };"  refer to?

what is {1} and {0}, If i write code like this how will browser know that which template to pick ?

If i assume {1} as controller name, I have 10′s of Classes in the Controller, so how would MVC pick the actually class and execute it ?

thank you so much for your help.

 

It uses the controller name in the route values collection. This collection was used to find the controller.

Hi sukumarchall,

Thanks for your post.

Default convention for views location is either ~/Views/{0}/{1}.{2} or
~/Views/Shared/{1}.{2}.  In convention {0} – represents controller name, {1} – represents action method name, {2} – represents extension of file name.The Razor engine reads view templates from a physical location on disk. The path is retrieved
using  the ASP.NET virtual path provider.

The location of view templates is rooted in the Views folder. The Views folder has a  number of subfolders—each named after an existing controller. Each controller-specific directory contains physical files  whose name is expected to match
the name of a view invoked from the controller.

More information,you can refer to the following link:

#Configure The Views Search Locations in ASP.NET MVC

http://theshravan.net/blog/configure-the-views-search-locations-in-asp-net-mvc/

 #Look at the Razor View Engine in ASP.NET MVC

https://www.simple-talk.com/dotnet/asp.net/a-look-at-the-razor-view-engine-in-asp.net-mvc/

Hope this can be helpful.

Best Regards,

Eileen

Leave a Reply