custom view engine
ASP.NET MVC がビューを検索する追加のフォルダーを指定できる場所を記述できます。
ここでの考え方は、次のRazorViewEngine
ようなさまざまなプロパティから派生するクラスを作成し、そのコンストラクターで設定することです。
- AreaViewLocationFormats
- AreaMasterLocationFormats
- AreaPartialViewLocationFormats
- ViewLocationFormats
- MasterLocationFormats
- PartialViewLocationFormats
以下は、自由にオーバーライドできるデフォルト値です。
public RazorViewEngine(IViewPageActivator viewPageActivator) : base(viewPageActivator)
{
base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
base.AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
base.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
base.FileExtensions = new string[] { "cshtml", "vbhtml" };
}
次に、カスタム ビュー エンジンを に登録しますApplication_Start
。
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MyRazorViewEngine());
この例では、カスタム ビュー エンジンを登録する前に、他のすべての既定のビュー エンジン (WebForms と Razor) を削除しました。