asp.net mvc Adding Custom direcotry for Views (asp.net mvc How to specify which folder the View pages reside in?)
Introduction :
In asp.net mvc by default when we create application, our Views reside in Views directory for our Controller actions. For Example, by default it create Home controller with Index action, and if we see in Solution Explorer in Views directory we can see directory Views -->> Home --> Index.cshtml and we have its action like this:
public class HomeController : Controller { public ActionResult Index() { return View(); } }
and we have this action's Views in Views folder see the following screen :
Now by default it will first look for Index.cshtml file in Views/Home folder and it is unable to find it there then it will find in View/Shared folder, if it also not finds it there it will throw exception that view file is not found, here is the exception text which is thrown:
The view 'Index' or its master was not found or no view
engine supports the searched locations. The following locations were
searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
See:
Using Custom Directories:
Views/Home/Partials and Views/Shared/Partial then we have to tell the ViewEngine to look in that directory as well by writing the following code in Gloabl.asax file in Application_Start event.
For Example i have this code and i am returning _LoginPartial.cshtml from Index action of HomeController , now what will happen it will look in View/Home directory first and in failure it will look in View/Shared , but this time i have my partial views in seperate directory named Partial for every controller and for shared as well, In this case HomeController partial views are in Views/Home/Partials and in Views/Shared/Partials:
public class HomeController : Controller { public ActionResult Index() { return View(); } }
The beauty of asp.net mvc framework is the extensiblity which you can do according to your needs and business requirements, one of them is that if you want your own directories structure for organizing your views you can register those directories with razor view engine, doing that will make your life easy as you will not have to specify fully qualified path of the view, as razor will know that it needs to look for the view in those directories as well which you have registered with it.
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); RazorViewEngine razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().FirstOrDefault(); if (razorEngine != null) { var newPartialViewFormats = new[] { "~/Views/{1}/Partials/{0}.cshtml", "~/Views/Shared/Partials/{0}.cshtml" }; razorEngine.PartialViewLocationFormats = razorEngine.PartialViewLocationFormats.Union(newPartialViewFormats).ToArray(); } } }