asp.net mvc - Order in which Views are searched in MVC -


in way views searched in route.config file. want order in views searched. e.g.

~/views//home/index ~/views/shared/home/index 

by default, mvc view engine searches available view cshtml files in these locations in order top bottom:

~/views/controllername/actionname.cshtml

~/views/shared/actionname.cshtml

~/views/shared/layoutname.cshtml (for layout files)

either changing or re-ordering view engine search method requires creating new class one:

public class customviewsearch : razorviewengine {     public customviewsearch()     {         masterlocationformats = new[]         {             "~/views/shared/{0}.cshtml"         };          viewlocationformats = new[]         {             // can change view search order here             // {0} = action name, {1} = controller name             "~/views/{1}/{0}.cshtml",             "~/views/shared/{1}/{0}.cshtml"         };         partialviewlocationformats = viewlocationformats;          fileextensions = new[]         {             "cshtml"         };     } } 

then, place custom view search method on global.asax inside application_start method:

protected void application_start() {     // remove existing view search methods if want     viewengines.engines.clear();       // add custom view search method here     viewengines.engines.add(new customviewsearch());  } 

any suggestions welcome.


Comments