所以我註冊的所有領域中Global.asax
:如何在我的區域內進行FindPartialView搜索?
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//...
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
但在我/Areas/Log/Controllers
,當我試圖找到一個PartialView
:
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "_LogInfo");
它失敗,viewResult.SearchedLocations
是:
"~/Views/Log/_LogInfo.aspx"
"~/Views/Log/_LogInfo.ascx"
"~/Views/Shared/_LogInfo.aspx"
"~/Views/Shared/_LogInfo.ascx"
"~/Views/Log/_LogInfo.cshtml"
"~/Views/Log/_LogInfo.vbhtml"
"~/Views/Shared/_LogInfo.cshtml"
"~/Views/Shared/_LogInfo.vbhtml"
因此viewResult.View
是null
。如何在我的區域搜索FindPartialView
?
更新: 這是我的自定義視圖引擎,這是我在Global.asax
已註冊:
public class MyCustomViewEngine : RazorViewEngine
{
public MyCustomViewEngine() : base()
{
AreaPartialViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
PartialViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
// and the others...
}
}
但FindPartialView
不使用AreaPArtialViewLocationFormats
:
"~/Views/Log/_LogInfo.cshtml"
"~/Views/Shared/_LogInfo.cshtml"
謝謝,能否再詳述一下?我現在有一個自定義視圖引擎,並設置了位置(請參閱我的更新),但FindPartialView不使用它們。任何指針? – 2013-03-16 20:42:36
如果您試圖在正常的MVC位置(即根視圖)中找到Area文件夾中的局部視圖,那麼我認爲您必須添加路徑(〜/ Areas/{2}/Views/{1}/{ 0} .cshtml)添加到PartialViewLocationFormats數組中。 – gdp 2013-03-16 21:56:15
我正在嘗試(〜/ Areas/{2}/Views/{1}/{0} .cshtml),顯示路徑,但viewresult返回值爲null。 – user2156088 2013-05-30 06:30:56