モバイル リクエストを処理するためのカスタム ビュー エンジンを作成しました。これらのビューには「.mobile」拡張子が必要で、/ViewsMobile ルート フォルダーの下に配置する必要があります。
public class MobileViewEngine : RazorViewEngine
{
public MobileViewEngine()
{
MasterLocationFormats = new string[] { "~/ViewsMobile/Shared/{0}.mobile" };
ViewLocationFormats = new string[] { "~/ViewsMobile/{1}/{0}.mobile", "~/ViewsMobile/Shared/{0}.mobile" };
PartialViewLocationFormats = new string[] { "~/ViewsMobile/Widgets/{1}/{0}.mobile" };
FileExtensions = new string[] { "mobile" };
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
ViewEngineResult result = null;
var request = controllerContext.HttpContext.Request;
if (request.Browser.IsMobileDevice)
{
result = base.FindView(controllerContext, viewName, masterName, false);
}
return null;
}
}
この ViewEngine を ViewEngines.Engines の位置 0 (最上位のエンジン) に挿入しましたApplication_Start
。
ViewEngines.Engines.Insert(0, new MobileViewEngine());
.mobile 拡張子を認識するために、次の行を web.config に追加した後:
<buildProviders>
<add extension=".mobile" type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</buildProviders>
ここで、モバイル プラットフォームからホームページ (コントローラー = ページ、アクション = メイン) にアクセスすると、次の例外が発生します。
「~/ViewsMobile/Pages/Main.mobile」のコード言語を特定できませんでした。オンラインでクラッシュする
base.FindView(controllerContext, viewName, masterName, false);
スタック トレースは次のとおりです。
[InvalidOperationException: "~/ViewsMobile/Pages/Main.mobile] のコード言語を特定できませんでした] System.Web.WebPages.Razor.WebPageRazorHost.GetCodeLanguage()+24401
System.Web.WebPages.Razor.WebPageRazorHost..ctor(String virtualPath, String physicalPath) +136
System.Web.Mvc.MvcWebRazorHostFactory.CreateHost(String virtualPath, String physicalPath) +43 ....
「.mobile」などのビューにカスタム拡張機能を使用し、それぞれの中で Razor を使用する方法を知っていますか?
前もって感謝します。
敬具。
ホセ。