Asp.net mvc ASP.NET MVC 2中的自定义视图引擎不适用于区域
到目前为止,我在ASP.NET MVC v1和v2中使用了下面的代码,但是当我今天在应用程序中添加一个区域时,该区域的控制器在我的Areas/views/controllerView文件夹中找不到任何视图。它发布了一个众所周知的例外,它搜索了这4个标准文件夹,但没有在区域下搜索 如何更改代码以使其与区域一起工作?可能是ASP.NET MVC 2下带有区域支持的自定义视图引擎的示例?网上关于它的信息非常令人震惊 代码如下:Asp.net mvc ASP.NET MVC 2中的自定义视图引擎不适用于区域,asp.net-mvc,viewengine,Asp.net Mvc,Viewengine,到目前为止,我在ASP.NET MVC v1和v2中使用了下面的代码,但是当我今天在应用程序中添加一个区域时,该区域的控制器在我的Areas/views/controllerView文件夹中找不到任何视图。它发布了一个众所周知的例外,它搜索了这4个标准文件夹,但没有在区域下搜索 如何更改代码以使其与区域一起工作?可能是ASP.NET MVC 2下带有区域支持的自定义视图引擎的示例?网上关于它的信息非常令人震惊 代码如下: public class PendingViewEngine : Virt
public class PendingViewEngine : VirtualPathProviderViewEngine
{
public PendingViewEngine()
{
// This is where we tell MVC where to look for our files.
/* {0} = view name or master page name
* {1} = controller name */
MasterLocationFormats = new[] {"~/Views/Shared/{0}.master", "~/Views/{0}.master"};
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.aspx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx",
"~/Views/{1}/{0}.ascx"
};
PartialViewLocationFormats = new[] {"~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.ascx"};
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new WebFormView(partialPath, "");
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return new WebFormView(viewPath, masterPath);
}
}
创建区域时,它是否创建了AreaRegistration类?如果是这样,您的
global.asax.cs
中是否有此项?顾名思义,它向MVC注册这些区域
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
}
创建区域时,它是否创建了AreaRegistration类?如果是这样,您的
global.asax.cs
中是否有此项?顾名思义,它向MVC注册这些区域
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
}
。。。搜索了这4个标准文件夹,但没有在区域下搜索
这实际上是一个提示-MVC不知道在哪里以及如何查找区域视图,因为您的自定义视图引擎中尚未定义位置
您可能需要设置AreaPartialViewLocationFormats
,并在ViewLocationFomats
属性中包含Areas
位置,因为这是一个启用了区域的应用程序
ViewLocationFormats = new[]
{
"~/Areas/Views/{1}/{0}.aspx",
...
};
而且可能
AreaPartialViewLocationFormats = new[]
{
"~/Areas/{1}/Views/{0}.ascx",
"~/Areas/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.ascx"
};
两份参考文件:
AreaPartialViewLocationFormats
,并在ViewLocationFomats
属性中包含Areas
位置,因为这是一个启用了区域的应用程序
ViewLocationFormats = new[]
{
"~/Areas/Views/{1}/{0}.aspx",
...
};
而且可能
AreaPartialViewLocationFormats = new[]
{
"~/Areas/{1}/Views/{0}.ascx",
"~/Areas/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.ascx"
};
两份参考文件:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new PendingViewEngine());
}
- 马特
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new PendingViewEngine());
}
- 马特
返回新的WebFormView(partialPath,“”)
to返回新的WebFormView(partialPath,null)代码>?我有这个,我有区域注册类,但问题仍然存在。问题是,“我的视图引擎”会重置文件夹的某些路径,以允许在其各自文件夹中的视图与共享文件夹之间进行链接,如您所见。在对其他视图引擎进行编码时,这些区域不相关。是默认的WebFormViewEngine,其中没有关于区域的代码。这就是我认为它可能与配置有关的原因。感谢您的建议,但这一定是视图引擎问题,因为如果我从global.asax.cs中对其进行注释,它会发现区域视图没有问题。您是否可以尝试更改返回新的WebFormView(partialPath,“”)
to返回新的WebFormView(partialPath,null)代码>?