【问题标题】:Route to Subdirectory in an Area路由到区域中的子目录
【发布时间】:2017-10-09 15:35:28
【问题描述】:

我已经创建了一个这样的区域:Admin/

我正在尝试在该区域内创建一个子目录:Admin/Permissions/ByGroup/,具有 Admin/Permissions/ByGroup/Edit/1 等功能。这是因为我将有其他方法来查看和编辑权限,例如 ByUser、ByActivity 等。

但是,我在正确路由到 ByGroupController 及其视图时遇到问题。 Admin/Admin/Permissions/(由 PermissionsController 触发)都可以正常工作。

导航到Admin/Permissions/ByGroup/ 时出现异常:

Server Error in '/' Application.

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

我的文件夹结构如下:

AdminAreaRegistration.cs

using System.Web.Mvc;

namespace MyMVCSite.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "ByGroup_default",
                "Admin/Permissions/ByGroup/{controller}/{action}/{id}",
                new { controller = "ByGroup", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MyMVCSite.Areas.Admin.Controllers" }
                );

            context.MapRoute(
                    "Permissions_default",
                    "Admin/Permissions/{controller}/{action}/{id}",
                    new { controller = "Permissions", action = "Index", id = UrlParameter.Optional },
                    namespaces: new[] { "MyMVCSite.Areas.Admin.Controllers" }
                    );

            context.MapRoute(
                 "Admin_default",
                 "Admin/{controller}/{action}/{id}",
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                 namespaces: new[] { "MyMVCSite.Areas.Admin.Controllers" }
            );




        }
    }
}

ByGroupController.cs

namespace MyMVCSite.Areas.Admin.Controllers
{
    public class ByGroupController : Controller
    {
        // GET: Admin/ByGroup
        public ActionResult Index()
        {
            return View();
        }

        // GET: Admin/ByGroup
        public ActionResult Edit()
        {
            return View();
        }
    }
}

除了简单的网站之外,我还没有真正将 MVC 用于任何其他网站,也没有用于具有如此多视图级别的网站。我要怎么解决这个错误,有没有更好的方法来组织带有视图子目录的区域?我真的不需要进行自定义路由,所以我的AdminAreaRegistration.cs 也可能不正确。

感谢任何帮助或指导。

【问题讨论】:

  • 路由用于传入请求。视图的选择完全基于约定(虽然我可以被 ViewEngine 覆盖)。
  • @ErikPhilips 那么我该如何深入到 Permissions 目录中,以找到具有自己的控制器/视图的其他目录?
  • 我想我已经明白你的意思了,@ErikPhilips。我现在保留我的目录结构,但不是进行路由,而是从我的ByGroupController 返回视图,例如return View("~/Areas/Admin/Views/Permissions/ByGroup/Index.cshtml");。这是正确的吗?

标签: c# asp.net-mvc url-routing asp.net-mvc-routing asp.net-mvc-areas


【解决方案1】:

默认情况下,路由只是一种抽象——也就是说,您的 URL 与控制器的物理位置完全无关。您可以使用路由以任何方式创建 URL,同时将控制器放置在您想要的任何位置。

另一方面,视图需要一些额外的工作才能放入不遵循约定的不同目录中。您可以通过更改视图引擎来实现 - 请参阅 Can I specify a custom location to "search for views" in ASP.NET MVC?

有一个第 3 方包可以帮助您实现该目标 - MvcCodeRouting 会根据您放置控制器的文件夹自动生成路由。但是,您仍然可以修改视图引擎以进行搜索对于视图,因为它们是完全独立的关注点。

【讨论】:

    猜你喜欢
    • 2018-05-20
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多