When compiling a program using ASP. net mvc Framework, we sometimes need to select the storage location of the view template based on the business, such as selecting different skins for user settings. As follows:
Create a storage path for Themes/{Channel}/{Theme}/{Controller}/{Action}. The view template also uses webform. In the Url client display, we also follow the {Channel}/{Controller}/{Action} rules.
The implementation idea is to rewrite the implementation to write a ThemeViewEngine and implement IViewEngine. The function is to select the correct WebForumView path.
See the implementation code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using System. Web. Hosting;
Namespace MvcApplicationTest. ThemeEngines
{
/// <Summary>
/// Skin selection Engine
/// </Summary>
Public class ThemeViewEngine: IViewEngine
{
# Region IViewEngine Member
Public ViewEngineResult FindView (ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
Return _ FindView (controllerContext, viewName, null, useCache, "aspx ");
}
Public ViewEngineResult FindPartialView (ControllerContext controllerContext, string partialViewName, bool useCache)
{
Return _ FindView (controllerContext, partialViewName, null, useCache, "ascx ");
}
Public void ReleaseView (ControllerContext controllerContext, IView view)
{
IDisposable disposable = view as IDisposable;
If (disposable! = Null)
Disposable. Dispose ();
}
# Endregion
Private ViewEngineResult _ FindView (ControllerContext controllerContext, string viewName, string masterName, bool useCache, string ext)
{
If (controllerContext = null)
Throw new ArgumentNullException ("controllerContext is required.", "controllerContext ");
If (string. IsNullOrEmpty (viewName ))
Throw new ArgumentException ("viewName is required.", "viewName ");
// The masterName of the View cannot be specified in the Controller.
If (! String. IsNullOrEmpty (masterName ))
{
Throw new ArgumentException ("masterName of View cannot be specified in Controller", "masterName ");
}
String selectedThemeName = GetSelectedTheme (controllerContext );
String controllerName = controllerContext. RouteData. Values ["controller"]. ToString ();
String viewPath = GetViewPath (controllerContext, selectedThemeName, controllerName, viewName, ext );
If (viewPath = null)
Throw new InvalidOperationException (String. Format ("The view '{0}' could not be located at these paths: {1}", viewName, GetChannelName (controllerContext )));
Return new ViewEngineResult (new WebFormView (viewPath, null), this );
}
# Region Help Methods
/// <Summary>
/// Obtain the path of the skin file
/// </Summary>
Private static string GetViewPath (ControllerContext controllerContext, string themeName, string controllerName, string viewName, string ext)
{
Return String. Format (@"~ /Themes/{0}/{1}/{2}/{3}. {4} ", GetChannelName (controllerContext), themeName, controllerName, viewName, ext );
}
Private static string GetChannelName (ControllerContext controllerContext)
{
If (controllerContext. RouteData. Values ["channel"] = null)
{
Return "";
}
Else
{
Return controllerContext. RouteData. Values ["channel"]. ToString ();
}
}
/// <Summary>
/// Obtain the name of the topic to be displayed
/// </Summary>
Private static string GetSelectedTheme (ControllerContext controllerContext)
{
String themeName = "Default ";
If (GetChannelName (controllerContext). ToLower () = "channel ")
{
ThemeName = "Default ";
}
Return themeName;
}
# Endregion
}
}
You also need to configure Global
Routes. MapRoute (
"Channel", // Route name
"{Channel}/{controller}/{action}/{id}", // URL with parameters
New {channel = "Channel", controller = "Home", action = "Index", id = ""} // Parameter defaults
);
ViewEngines. Engines. Add (new ThemeEngines. ThemeViewEngin