2009-05-02 73 views
0

重要更新
由於MVC 2.0預覽1此功能的發佈,已經實現在區域的形式實際框架本身的一部分。更多詳細信息請參閱Phil Haack的博客here如何指定一個Mvc視圖將爲Mvc控制器創建的位置?

我有一個名爲ListManagerController的控制器。該控制器包含一個名爲Index()的ActionResult方法。當我在Visual Studio中右鍵選擇索引並選擇添加視圖時,新視圖將在/ Views/ListManager/Index中創建。

但是,我希望在/ Views/管理/ListManager /中創建索引視圖和所有後續視圖。我怎麼做到這一點?

編輯:有人指出,這個問題是發佈的問題的副本here。看起來我的搜索技能最初失敗了。

+0

是否需要Visual Studio的設置才能在該文件夾中創建視圖,或者您希望MVC框架在查看文件夾下的文件夾中查找視圖? – 2009-05-02 01:25:37

+0

這個問題暗示了這兩種情況,但這是由於缺乏理解,因爲我認爲創建視圖的位置會自動定義到視圖的路徑。 – BinaryMisfit 2009-05-02 07:32:10

+0

我最終重構了網站適用於vanilla Mvc架構的方式。然而,兩個答案都爲上述問題提供了可行的解決方案。 – BinaryMisfit 2009-05-02 10:54:10

回答

3

視圖的位置與您正在使用的ViewFactory綁定。 AFAIK Web表單視圖引擎不支持區域[在您的示例中管理]。

Spark支持此功能並且非常乾淨,您還可以混合搭配網頁表單和火花視圖,因此您不必重新創建所有視圖。

更新:看起來像菲爾哈克有一個blog post如何實現這一目標。他的代碼是針對RC的,但我認爲這應該可以很好地對ASP.NET MVC RTM進行編譯。

0

這個問題是非常多的this question

重複,所以我會引用我的回答是一個在這裏。

我想出了不同的解決方案 ,並不需要我來推出自己的 視圖引擎。

基本上,我想保持MVC的驅動儘可能 「公約」,但我還是 想組織所有的我 「管理」視圖的〜/查看/管理 文件夾下。

例子:

  • 〜/查看/管理/用戶/
  • 〜/查看/管理/新聞/
  • 〜/查看/管理/博客/

我的解決辦法是爲我的特定管理員創建一個新的基類 類,並將該控制器的視圖的「路徑」強制爲 。

我有一個博客帖子和示例代碼 這裏:Organize your views in ASP.Net MVC

1

我知道你已經接受一個答案,但這裏是我想出了同時用同樣的想法實驗,用Phil Haack's post幫助。

首先,您需要擁有自己的ViewEngine才能在查看文件夾下查找文件夾。像這樣的東西(你會發現,它看起來很像菲爾哈克的區域編碼)

public class TestViewEngine : WebFormViewEngine 
{ 
    public TestViewEngine() 
     : base() 
    { 
     MasterLocationFormats = new[] { 
      "~/Views/{1}/{0}.master", 
      "~/Views/Shared/{0}.master" 
     }; 

     ViewLocationFormats = new[] { 
      "~/{0}.aspx", 
      "~/{0}.ascx", 
      "~/Views/{1}/{0}.aspx", 
      "~/Views/{1}/{0}.ascx", 
      "~/Views/Shared/{0}.aspx", 
      "~/Views/Shared/{0}.ascx" 
     }; 

     PartialViewLocationFormats = ViewLocationFormats; 
    } 
    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) 
    { 
     ViewEngineResult rootResult = null; 

     //if the route data has a root value defined when mapping routes in global.asax 
     if (controllerContext.RouteData.Values.ContainsKey("root")) { 
      //then try to find the view in the folder name defined in that route 
      string rootViewName = FormatViewName(controllerContext, viewName); 
      rootResult = base.FindView(controllerContext, rootViewName, masterName, useCache); 
      if (rootResult != null && rootResult.View != null) { 
       return rootResult; 
      } 
      //same if it's a shared view 
      string sharedRootViewName = FormatSharedViewName(controllerContext, viewName); 
      rootResult = base.FindView(controllerContext, sharedRootViewName, masterName, useCache); 
      if (rootResult != null && rootResult.View != null) { 
       return rootResult; 
      } 
     } 
     //if not let the base handle it 
     return base.FindView(controllerContext, viewName, masterName, useCache); 
    } 

    private static string FormatViewName(ControllerContext controllerContext, string viewName) { 
     string controllerName = controllerContext.RouteData.GetRequiredString("controller"); 

     string root = controllerContext.RouteData.Values["root"].ToString(); 
     return "Views/" + root + "/" + controllerName + "/" + viewName; 
    } 

    private static string FormatSharedViewName(ControllerContext controllerContext, string viewName) { 
     string root = controllerContext.RouteData.Values["root"].ToString(); 
     return "Views/" + root + "/Shared/" + viewName; 
    } 
} 

然後在你的Global.asax與您的自定義一個替換默認視圖引擎,在Application_Start

ViewEngines.Engines.Clear(); 
ViewEngines.Engines.Add(new TestViewEngine()); 

現在,當你在Global.asax確定的路線,你需要設置一個root值,指示該文件夾尋找下查看文件夾,如下所示:

routes.MapRoute(
    "ListManager", 
    "ListManager/{action}/{id}", 
    new { controller = "ListManager", action = "Index", id = "", root = "Manage" } 
); 
相關問題