2011-09-13 15 views
1

従来のWebフォームビューを使用する古いASP.NET MVCアプリケーションがあります。実験として、私たちはいくつかのかみそりの意見を取り入れ始めました。残念ながら、目的のビューを見つける場所のデフォルトの優先順位は、私が望むものではありません。 MVCはまず、/ Views/ControllerNameフォルダ内でaspxファイルとascxファイルを探します。次に、aspxとascxファイルの/ Views/Sharedに移動します。次に、.cshtmlファイルと.vbhtmlファイルを探します。私が望むのは、/ Views/ControllerNameフォルダ内のすべての可能性がなくなるまで、Sharedフォルダに入れないことです。これはどうすればいいですか?Views/Sharedフォルダ内のrazorビューとwebformsビューのMVCを最後に探す方法を教えてください。

--- --- UPDATE

は、ここで私は後だ何を説明するのに役立つかもしれないいくつかの追加情報です。

~/Views/Home/Index.aspx 
~/Views/Home/Index.ascx 
~/Views/Shared/Index.aspx 
~/Views/Shared/Index.ascx 
~/Views/Home/Index.cshtml 
~/Views/Home/Index.vbhtml 
~/Views/Shared/Index.cshtml 
~/Views/Shared/Index.vbhtml 

私が欲しい本です:私は、この検索順序が取得デフォルトでは、言い換えれば

~/Views/Home/Index.aspx 
~/Views/Home/Index.ascx 
~/Views/Home/Index.cshtml 
~/Views/Home/Index.vbhtml 
~/Views/Shared/Index.aspx 
~/Views/Shared/Index.ascx 
~/Views/Shared/Index.cshtml 
~/Views/Shared/Index.vbhtml 

それは完全に/ビュー/コントローラフォルダを検索する前に、それが共有検索しないはずです。

答えて

0

を提出あなたのglobal.asax.csのビューエンジンの優先順位を設定することができ、私はシンプルな流暢APIを望んでいたものを達成することができました。拡張メソッドは、私が各ビューエンジンから望まない検索場所を削除します。検索場所は、.ViewLocationFormats.PartialViewLocationFormats文字列配列に格納されます。だからここに、これらの配列から不要な項目を削除流暢APIです:

public static class BuildManagerViewEngineFluentExtensions { 
    public static BuildManagerViewEngine ControllerViews(this BuildManagerViewEngine engine) { 
     return FilterViewLocations(engine, x => x.Contains("/Views/Shared/") == false); 
    } 

    public static BuildManagerViewEngine SharedViews(this BuildManagerViewEngine engine) { 
     return FilterViewLocations(engine, x => x.Contains("/Views/Shared/") == true); 
    } 

    private static BuildManagerViewEngine FilterViewLocations(BuildManagerViewEngine engine, Func<string, bool> whereClause) { 
     engine.ViewLocationFormats = engine.ViewLocationFormats.Where(whereClause).ToArray(); 
     engine.PartialViewLocationFormats = engine.PartialViewLocationFormats.Where(whereClause).ToArray(); 
     return engine; 
    } 
} 

はそして、私のglobal.asaxで、私は問題を逆にprotected void Application_Start()

ViewEngines.Engines.Clear(); 
ViewEngines.Engines.Add(new RazorViewEngine().ControllerViews()); 
ViewEngines.Engines.Add(new WebFormViewEngine().ControllerViews()); 
ViewEngines.Engines.Add(new RazorViewEngine().SharedViews()); 
ViewEngines.Engines.Add(new WebFormViewEngine().SharedViews()); 
3

あなたはそれを少しいじる後

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 

     ViewEngines.Engines.Clear(); 
     ViewEngines.Engines.Add(new RazorViewEngine()); 
     ViewEngines.Engines.Add(new WebFormViewEngine()); 
    } 
+0

に次の行を追加しました。最初に.aspx/.ascxから.cshtml/.vbhtmlに順番を変更します。しかし、それは/ Views/ControllerNameフォルダ内のすべての可能性を使い果たす前に、Sharedフォルダをまだ見ているので、これは私が探しているものではありません。 – mattmc3

+0

@ mattmc3あなたが求めるものを実現するには、 'RazorViewEngine'と' WebFormViewEngine'へのプロキシである新しいView Engineを書く必要があります。 – Eranga