所以我注册的所有领域中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