Asp.net mvc 如何在我的区域内进行FindPartialView搜索?

Asp.net mvc 如何在我的区域内进行FindPartialView搜索?,asp.net-mvc,Asp.net Mvc,因此,我在Global.asax中注册所有区域: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); //... RouteConfig.RegisterRoutes(RouteTable.Routes); } public class MyCustomViewEngine : RazorViewEngine { public MyCustomViewEngine() : base(

因此,我在
Global.asax
中注册所有区域:

protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();
  //...
  RouteConfig.RegisterRoutes(RouteTable.Routes);
}
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...
  }
}
但是在我的
/Areas/Log/Controllers
中,当我尝试查找
部分视图时:

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中注册的自定义视图引擎:

protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();
  //...
  RouteConfig.RegisterRoutes(RouteTable.Routes);
}
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"

我遇到了完全相同的问题,我使用了一个中央Ajax控制器,在该控制器中,我从不同的文件夹/位置返回不同的局部视图

您需要做的是创建一个新的
ViewEngine
,它派生自
RazorViewEngine
(我假设您正在使用Razor),并在构造函数中显式地包含新的位置以搜索其中的部分

或者,您可以覆盖
FindPartialView
方法。默认情况下,
共享
文件夹和当前控制器上下文中的文件夹用于搜索

下面是一个演示如何覆盖自定义
RazorViewEngine
中的特定属性的示例

更新

您应该在PartialViewLocationFormats数组中包含partial的路径,如下所示:

public class MyViewEngine : RazorViewEngine
{
  public MyViewEngine() : base()
  {
    PartialViewLocationFormats = new string[]
     {
       "~/Area/{0}.cshtml"
       // .. Other areas ..
     };
  }
}
同样,如果要在
区域
文件夹内的控制器中查找局部视图,则必须将标准局部视图位置添加到
区域局部视图位置格式
数组中。我已经测试过了,它对我有效

只需记住将新的
RazorViewEngine
添加到您的
Global.asax.cs
,例如:

protected void Application_Start()
{
  // .. Other initialization ..
  ViewEngines.Engines.Clear();
  ViewEngines.Engines.Add(new MyViewEngine());
} 
以下是如何在名为“Home”的示例性控制器中使用它:


我已将要查找的部分存储在
/Area/Partial1.cshtml
路径中。

谢谢,您能进一步说明吗?我现在有了一个自定义视图引擎,并设置了位置(请参见我的更新),但FindPartialView没有使用它们。任何点?如果您试图从普通MVC位置(即根视图)在Area文件夹中查找部分视图,那么我认为您必须将路径(~/Areas/{2}/views/{1}/{0}.cshtml)添加到PartialViewLocationFormats数组中。我正在尝试(~/Areas/{2}/views/{1}/{0}.cshtml),显示路径,但viewresult返回值为空。另一个提示是,
PartialViewLocationFormats
还应包含“~/Area/{0}”的变体,似乎没有按照默认值添加下划线,尽管基于下划线的格式甚至被“新建文件向导”用于局部设置。这是我在项目中强加的命名约定。