modify asp.net mvc view naming convention
In asp.net mvc 3, View() method will select a view to render by following this naming convention
\Views\Controller\Action.cshtml
How can I modify the naming convention so that it will return Views开发者_如何学C from:
\Views\Theme\Controller\Action.cshtml
Theme
is obtained from Session
var theme = Session["SelectedTheme"] ?? "Default";
and here is the current folder structure
- ...
-Controllers
|-HomeController
|-AccountController
- ...
-Views
|-ThemeABC
|-Home
|-Account
|-ThemeXYZ
|-Home
|-Account
|-Default
|-Home
|-Account
- ...
I found this post very helpful. from his code, I just need to modify LocationFormats in constructor as follows.
public class AndraViewEngine : RazorViewEngine
{
public AndraViewEngine()
{
base.ViewLocationFormats = new string[] {
"~/Views/{2}/{1}/{0}.cshtml",
"~/Views/{2}/Shared/{0}.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
base.PartialViewLocationFormats = new string[] {
"~/Views/{2}/{1}/{0}.cshtml",
"~/Views/{2}/Shared/{0}.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
}
/* the rest of the code in that post */
}
精彩评论