【问题标题】:Error in displaying area's views along pages in Views folder沿 Views 文件夹中的页面显示区域视图时出错
【发布时间】:2018-01-22 11:16:21
【问题描述】:

我有一个包含大量控制器、模型和页面的 MVC 5 项目。为了使它们更易于维护,我将一些相关文件移到了一个名为 Mail 的新区域。

项目结构如下:

Automation (project's name)
 |- Controllers
 |- Views
 |- Models
 └── Areas  
      └── Mail
          └── Models 
          └── Controllers
               └── MailboxController
          └── Views
               └── Inbox.cshtml

为了能够加载新移动的视图,我执行了以下步骤:

  • 在区域 -> 邮件 -> 控制器中更改控制器的命名空间
  • 将 Rout 添加到 MailAreaRegistaration.cs,如下所示:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Mail_default",
            "Mail/{controller}/{action}/{id}",
            new { action = "Inbox", id = UrlParameter.Optional},
            new [] { "Automation.Areas.Mail.Controllers" });
    }
    
  • 在 global.asax.cs 中注册AllAreaas()
  • 在项目的 RoutConfig 文件中更改 MapRout 方法如下:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Dashboards", action = "Dashboard_Main", id = UrlParameter.Optional },
            namespaces: new [] {"Automation.Controllers"}
        );
    }
    

主视图文件夹中的所有页面都按预期呈现,但是当我想在邮件区域加载 inbox.cshtml 时,会显示此错误:

The view 'inbox' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/Mail/Views/mailbox/inbox.aspx
~/Areas/Mail/Views/mailbox/inbox.ascx
~/Areas/Mail/Views/Shared/inbox.aspx
~/Areas/Mail/Views/Shared/inbox.ascx
~/Views/mailbox/inbox.aspx
~/Views/mailbox/inbox.ascx
~/Views/Shared/inbox.aspx
~/Views/Shared/inbox.ascx
~/Areas/Mail/Views/mailbox/inbox.cshtml
~/Areas/Mail/Views/mailbox/inbox.vbhtml
~/Areas/Mail/Views/Shared/inbox.cshtml
~/Areas/Mail/Views/Shared/inbox.vbhtml
~/Views/mailbox/inbox.cshtml
~/Views/mailbox/inbox.vbhtml
~/Views/Shared/inbox.cshtml
~/Views/Shared/inbox.vbhtml

我在下面的收件箱页面的网址中测试过,但没有一个成功

我该如何解决?谢谢

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-areas


    【解决方案1】:

    假设你有这样的MailboxController 类:

    public class MailboxController : Controller
    {
        // default action in area
        public ActionResult Inbox()
        {
            return View();
        }
    }
    

    首先,创建自定义类以在以下示例中给出的区域文件夹中执行视图搜索:

    namespace Automation
    {    
        public class CustomRazorEngine : RazorViewEngine
        {
            public CustomRazorEngine()
            {
                // short note:
                // {2} = area name
                // {1} = controller name
                // {0} = action name
                AreaViewLocationFormats = new[]
                {
                    // add these routes in area scope
                    "~/Areas/{2}/Views/{0}.cshtml",
                    "~/Areas/{2}/Views/{1}/{0}.cshtml",
                };
    
                ViewLocationFormats = new[]
                {
                    // example routes
                    "~/{1}/{0}.cshtml",
                    "~/Views/{1}/{0}.cshtml"
                }
    
                FileExtensions = new[]
                {
                    "cshtml"
                };
            }
        }
    }
    

    然后我在 Global.asax 中的Application_Start 方法中注册如上所述的自定义视图引擎:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ViewEngines.Engines.Add(new CustomRazorEngine()); // customized view search engine
        // other settings here
    }
    

    通过以上这些 URL 的设置应该可以工作:

    http://localhost:port/Mail/Mailbox
    http://localhost:port/Mail/Mailbox/Inbox
    

    参考资料:

    Custom View Engine with Dynamic View Location

    ASP.NET MVC Custom View Routing

    【讨论】:

      【解决方案2】:

      解决此问题的最简单方法是遵循 MVC 约定并将 Inbox.cshtml 文件移动到 MVC 预期的 Areas\Mail\Views\Mailbox\Inbox.cshtml 位置(您只缺少 Mailbox 文件夹)。

      但是,如果您打算根据自己的喜好自定义查看位置,请使用Tetsuya's solution

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-11
        • 2013-11-23
        • 1970-01-01
        • 1970-01-01
        • 2015-10-07
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        相关资源
        最近更新 更多