到目前为止,我在 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);
}
}