轉帖|其它|編輯:郝浩|2009-03-10 10:07:19.000|閱讀 1229 次
概述:asp.net mvc 皮膚的探索實現
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在利用ASP.NET MVC 框架編寫程序時,有時我們需要根據業務自己選擇視圖模板存放的位置,比如針對用戶的設置選擇不同的皮膚。如下:
我們建一個 Themes/{Channel}/{Theme}/{Controller}/ {Action} 的存放路徑,視圖模板還用webform。 在Url客戶端的顯示上,我們還遵循{Channel}/{Controller}/ {Action}規則。
實現思路是重寫實現寫一個ThemeViewEngine, 實現IViewEngine,作用是選擇正確的WebForumView 的路徑。
看下實現代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Hosting;
namespace MvcApplicationTest.ThemeEngines
{
/// <summary>
/// 皮膚選擇引擎
/// </summary>
public class ThemeViewEngine:IViewEngine
{
#region IViewEngine 成員
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");
//當前不允許在Controller中指定View的masterName
if (!string.IsNullOrEmpty(masterName))
{
throw new ArgumentException("不允許在Controller中指定View的masterName", "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>
/// 獲取皮膚文件路徑
/// </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>
/// 獲取需要顯示的主題名稱
/// </summary>
private static string GetSelectedTheme(ControllerContext controllerContext)
{
string themeName = "Default";
if (GetChannelName(controllerContext).ToLower() == "channel")
{
themeName = "Default";
}
return themeName;
}
#endregion
}
}
在應用時,還需配置Global
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園