ASP.NET MVC 2 中的自定义视图引擎不适用于区域

发布于 2024-08-31 18:29:29 字数 1355 浏览 8 评论 0原文

到目前为止,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耳根太软 2024-09-07 18:29:29

不是对您的问题的直接回答,但其他读者可能会发现有用的东西,要使用自定义视图引擎,需要修改 global.asax:

 void Application_Start(object sender, EventArgs e)
 {
  RegisterRoutes(RouteTable.Routes);
  ViewEngines.Engines.Clear();
  ViewEngines.Engines.Add(new PendingViewEngine());
 } 
  • Matt

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:

 void Application_Start(object sender, EventArgs e)
 {
  RegisterRoutes(RouteTable.Routes);
  ViewEngines.Engines.Clear();
  ViewEngines.Engines.Add(new PendingViewEngine());
 } 
  • Matt
山田美奈子 2024-09-07 18:29:29

...搜索了这 4 个标准文件夹,但它没有在区域下查找

这实际上是一个提示 - MVC 不知道在哪里以及如何查找区域视图,因为这些位置尚未在您的文件中定义自定义视图引擎。

您可能需要设置 AreaPartialViewLocationFormats 并将 Areas 位置包含在 ViewLocationFomats 属性中,因为这是一个启用区域的应用程序。

ViewLocationFormats = new[]
{
   "~/Areas/Views/{1}/{0}.aspx",
   ...
};

并且可能...

AreaPartialViewLocationFormats = new[]
{
    "~/Areas/{1}/Views/{0}.ascx",
    "~/Areas/Views/{1}/{0}.ascx",
    "~/Views/Shared/{0}.ascx"
};

两个参考:

  1. MSDN <- 可能已更新
    自 MVC1 以来包含新区域
    东西,因此为什么它不起作用
  2. The Haack <- 老帖子,但很好的介绍和概述

... 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 the Areas Location in the ViewLocationFomats property since this is an area enabled application.

ViewLocationFormats = new[]
{
   "~/Areas/Views/{1}/{0}.aspx",
   ...
};

And possibly...

AreaPartialViewLocationFormats = new[]
{
    "~/Areas/{1}/Views/{0}.ascx",
    "~/Areas/Views/{1}/{0}.ascx",
    "~/Views/Shared/{0}.ascx"
};

Two references:

  1. MSDN <- Probably updated
    since MVC1 to include the new Areas
    stuff, thus why its not working
  2. The Haack <- Old post but a good intro and overview
静若繁花 2024-09-07 18:29:29

当您创建 Area 时,它是否创建了 AreaRegistration 类?如果是这样,您的 global.asax.cs 中是否有此内容?顾名思义,它向 MVC 注册区域。

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    }

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.

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文