2013-08-28 75 views
0

我有一個MVC4應用程序,目前使用一個區域(除了正常的位置)。通常情況下,我的路由一切正常,但我現在需要在「根」位置的代碼來查找位於該區域內的局部視圖。指定區域搜索視圖

我的解決方案的文件中的相關部分:

/Areas 
/Areas/Admin 
/Areas/Admin/Views 
/Areas/Admin/Views/Shared/_adminPartialView.cshtml 
/Views 
/Views/Shared/_rootPartialView.cshtml 

而我失敗的代碼是:

var viewEngine = new RazorViewEngine(); 
var cContext = new ControllerContext(context, new RouteData(), new EmptyController()); 

// this works: 
var rootView = viewEngine.FindPartialView(cContext, "_rootPartialView", false); 
if (rootView == null) { throw new Exception("Root view not found"); } 

// this throws my error: 
var adminView = viewEngine.FindPartialView(cContext, "_adminPartialView", false); 
if (adminView == null) { throw new Exception("Admin view not found"); } 

context以上從HttpContext.Current間接上市)

.. 。這是有道理的,沒有一個區域指定管理區域不應該被搜查......但是當我將上面的viewEngine更改爲= new AdminRazorViewEngine();,其定義如下:

public class AdminRazorViewEngine : RazorViewEngine { 
    public AdminRazorViewEngine() { 
     var viewLocations = new [] { 
      "~/Areas/Admin/Views/{1}/{0}.cshtml", 
      "~/Areas/Admin/Views/Shared/{0}.cshtml" 
     }; 
     base.ViewLocationFormats = viewLocations.Concat(base.ViewLocationFormats).ToArray(); 
    } 
} 

我仍然得到相同的結果。

如果我將_adminPartialView.cshtml複製到/Views/Shared/那麼使用任一視圖引擎都可以正確找到兩個視圖。

我也嘗試創建一個實際的RouteData對象,指定它的面積,並使用該構建我的ControllerContext時:

var routeData = new RouteData(); 
routeDate.Values.Add("area", "Admin"); 
// ... 
var cContext = new ControllerContext(context, routeData, new EmptyController()); 

隨着行爲沒有變化。

如何顯式指定視圖引擎的區域進行搜索?

回答

0

嘗試設置AreaViewLocationFormats代替ViewLocationFormats

下面是從ILSpy代碼爲RazorViewEngine

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" 
     }; 
    } 
+1

D'哦。我正在尋找部分視圖,但不添加到PartialViewLocationFormats – DaveD