Is it possible to define more folders for ASP.NET MVC to search for Views or Partials?
For example, if I browse to /Home/Index and the Index action returns View(), ASP.NET MVC will look at the following locations:
- ~/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
I want to create another folder, say ~/Views/PartivalViews/, that will be searched.
Obviously I’m looking for this as a tidy way to store my PartialViews.
You could write a
custom view enginewhere you could specify additional folders where ASP.NET MVC will look for views.The idea here is to write a class deriving from
RazorViewEngineand in its constructor set the various properties such as:Here are the default values that you could feel free to override:
And then simply register your custom view engine in
Application_Start:In this example I have removed all other default view engines (WebForms and Razor) before registering our custom one.