1. 先看下webformviewengine的建構函式:public WebFormViewEngine()
{
base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.master", "~/Views/Shared/{0}.master" };
base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.aspx", "~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx" };
base.PartialViewLocationFormats = base.ViewLocationFormats;
}
base.ViewLocationFormats 可以看出view page為什麼只能寫在views檔案夾下的原因了。所以我們只需要在新的view engine的建構函式中修改下base.ViewLocationFormats 路徑即可。
2. 下面是一個自訂的View Engine -- SkinSupportViewEngine,它需要繼承WebFormViewEngine。
public class SkinSupportViewEngine : WebFormViewEngine
{
public SkinSupportViewEngine()
{
string[] mastersLocation = new[]
{
string.Format("~/skins/{0}/{0}.master",
Utils.SkinName)
};
MasterLocationFormats = this.AddNewLocationFormats(new List<string>(MasterLocationFormats),
mastersLocation);
string[] viewsLocation = new[]
{
string.Format("~/skins/{0}/Views/{{1}}/{{0}}.aspx",
Utils.SkinName),
string.Format("~/skins/{0}/Views/{{1}}/{{0}}.ascx",
Utils.SkinName)
};
ViewLocationFormats =
PartialViewLocationFormats = this.AddNewLocationFormats(new List<string>(ViewLocationFormats),
viewsLocation);
}
......
}
3. 最後還需要在Global 檔案中註冊新的View Engine
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new SkinSupportViewEngine());