ASP.NET MVC 多套皮肤解决方案
原理自己写了一个ThemeController继承于Controller,先看我的文件结构:
ThemeController的具体实现:
public class ThemeController : Controller
{
private static string[] sPathTemplate = new string[]{
"/Themes/{0}/{1}/{2}.aspx",
"/Themes/{0}/Shared/{1}.Master"};
private static ThemeController m_ThemeController = new ThemeController();
public static ViewResult View(string sTheme, string sController, string sView, string sMaster, object oModel)
{
string sViewPath = string.Format(sPathTemplate[0], sTheme, sController, sView);
string sMasterPath = string.Format(sPathTemplate[1], sTheme, sMaster);
return m_ThemeController.View(sViewPath, sMasterPath, oModel);
}
public static ViewResult View(string sTheme, string sController, string sView, object oModel)
{
return View(sTheme, sController, sView,"Site" , oModel);
}
public static ViewResult View(string sTheme, string sController, string sView)
{
string sViewPath = string.Format(sPathTemplate[0], sTheme, sController, sView);
string sMasterPath = string.Format(sPathTemplate[1], sTheme, "Site");
return m_ThemeController.View(sViewPath, sMasterPath);
}
//protected override ViewResult View(string viewName, string masterName, object model)
//{
// if (viewName == null && model != null)
// viewName = model.GetType().Name.ToLower().Replace("model", "view");
// return base.View(viewName, masterName, model);
//}
}
{
private static string[] sPathTemplate = new string[]{
"/Themes/{0}/{1}/{2}.aspx",
"/Themes/{0}/Shared/{1}.Master"};
private static ThemeController m_ThemeController = new ThemeController();
public static ViewResult View(string sTheme, string sController, string sView, string sMaster, object oModel)
{
string sViewPath = string.Format(sPathTemplate[0], sTheme, sController, sView);
string sMasterPath = string.Format(sPathTemplate[1], sTheme, sMaster);
return m_ThemeController.View(sViewPath, sMasterPath, oModel);
}
public static ViewResult View(string sTheme, string sController, string sView, object oModel)
{
return View(sTheme, sController, sView,"Site" , oModel);
}
public static ViewResult View(string sTheme, string sController, string sView)
{
string sViewPath = string.Format(sPathTemplate[0], sTheme, sController, sView);
string sMasterPath = string.Format(sPathTemplate[1], sTheme, "Site");
return m_ThemeController.View(sViewPath, sMasterPath);
}
//protected override ViewResult View(string viewName, string masterName, object model)
//{
// if (viewName == null && model != null)
// viewName = model.GetType().Name.ToLower().Replace("model", "view");
// return base.View(viewName, masterName, model);
//}
}
调用方法:
[HandleError]
public class HomeController : ThemeController
{
public ActionResult Index()
{
string m_sTheme = "default"; //默认的皮肤
if (Request.QueryString["Theme"] != null)
{
m_sTheme = Request.QueryString["Theme"];
}
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View(m_sTheme, "Home", "Index");
}
public ActionResult About()
{
return View();
}
}
public class HomeController : ThemeController
{
public ActionResult Index()
{
string m_sTheme = "default"; //默认的皮肤
if (Request.QueryString["Theme"] != null)
{
m_sTheme = Request.QueryString["Theme"];
}
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View(m_sTheme, "Home", "Index");
}
public ActionResult About()
{
return View();
}
}
显示效果:
自己觉得这是一个比较粗浅的解决方案,算是抛砖引玉吧,敬请多多拍砖!
更好的解决方案:重写视图引擎 附上源代码
--------------------------------------------------------------------------
Daniel Chow's Blog - 不管你在哪里,都要有一颗创业的心!
http://www.cnblogs.com/DanielChow/