4

MVC 4では、任意のビューに.Mobileを追加するだけで、モバイルデバイスは同じコントローラーからそのビューを自動的に提供されます。.Mobileファイルを別のフォルダーに保存する方法はありますか?デスクトップファイルをある「エリア」に保存し、モバイルを別の「エリア」に保存したいのです。誰かこのようなことを知っていますか?

4

1 に答える 1

2

これは、RazorViewEngine のカスタム実装を作成し、カスタム マッピングを ViewLocationFormats に追加することで簡単に実現できます。カスタム マッピングは既存のマッピングよりも具体的であるため、ViewLocationFormats 配列の先頭に追加することを忘れないでください。

namespace MobileViewsInMobileFolder.Utility {

    public class MyCustomViewEngine : RazorViewEngine {

         public MyCustomViewEngine() {
             List<string> existingViewLocationFormats = ViewLocationFormats.ToList();

             //Folder Structure: Views\Home\Desktop and Views\Home\Mobile
             existingViewLocationFormats.Insert(0, "~/Views/{1}/Desktop/{0}.cshtml");
             existingViewLocationFormats.Insert(0, "~/Views/{1}/Mobile/{0}.cshtml");

             //Folder Structure: Views\Desktop\Home and Views\Mobile\Home
             existingViewLocationFormats.Insert(0, "~/Views/Desktop/{1}/{0}.cshtml");
             existingViewLocationFormats.Insert(0, "~/Views/Mobile/{1}/{0}.cshtml");

             ViewLocationFormats = existingViewLocationFormats.ToArray();
         }
    }
}

そして、必ず Application_Start にカスタム ビュー エンジンを追加してください。

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MyCustomViewEngine());
于 2012-07-16T12:20:20.760 に答える