ASP.NET MVC 2 中的自定义视图引擎不适用于区域
到目前为止,我在 ASP.NET MVC v1 和 v2 中使用了下面的代码,但是当我今天向应用程序添加一个区域时,该区域的控制器在我的 Areas/Views/controllerView 文件夹中找不到任何视图。它发出了一个众所周知的异常,它搜索了这 4 个标准文件夹,但没有在区域下查找。
我如何更改代码以便它可以与区域一起使用?也许是 ASP.NET MVC 2 下具有区域支持的自定义视图引擎的示例?网上关于它的信息非常少。
代码如下:
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);
}
}
I used the code below so far with ASP.NET MVC v1 and v2 but when I added an Area today to my application, the controller for the area couldn't find any views in my Areas/Views/controllerView folder. It issued the very well known exception that it searched those 4 standard folders but it did not look under Areas..
How can I change the code so that it will work with Areas? Maybe an example of custom view engine under ASP.NET MVC 2 with Areas support? The information about it on the net is very scarse..
Here's the code:
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是对您的问题的直接回答,但其他读者可能会发现有用的东西,要使用自定义视图引擎,需要修改 global.asax:
Not a direct response to your question, but something other readers might find useful, to use the custom viewengine the global.asax needs to be modified:
...搜索了这 4 个标准文件夹,但它没有在区域下查找
这实际上是一个提示 - MVC 不知道在哪里以及如何查找区域视图,因为这些位置尚未在您的文件中定义自定义视图引擎。
您可能需要设置
AreaPartialViewLocationFormats
并将Areas
位置包含在ViewLocationFomats
属性中,因为这是一个启用区域的应用程序。并且可能...
两个参考:
自 MVC1 以来包含新区域
东西,因此为什么它不起作用
... searched those 4 standard folders but it did not look under Areas
This is actually a hint - MVC does not know where and how to look for the Area Views since the locations have not been defined in your custom view engine.
You may need to possibly setup the
AreaPartialViewLocationFormats
and include theAreas
Location in theViewLocationFomats
property since this is an area enabled application.And possibly...
Two references:
since MVC1 to include the new Areas
stuff, thus why its not working
当您创建 Area 时,它是否创建了 AreaRegistration 类?如果是这样,您的
global.asax.cs
中是否有此内容?顾名思义,它向 MVC 注册区域。When you created an Area, did it create the AreaRegistration class? If so, do you have this in your
global.asax.cs
? As the name implies, it registers the areas with MVC.