C# 路由到区域中的子目录
我已经创建了这样一个区域:C# 路由到区域中的子目录,c#,asp.net-mvc,url-routing,asp.net-mvc-routing,asp.net-mvc-areas,C#,Asp.net Mvc,Url Routing,Asp.net Mvc Routing,Asp.net Mvc Areas,我已经创建了这样一个区域:Admin/ 我正试图在该区域内创建一个子目录:Admin/Permissions/ByGroup/,具有Admin/Permissions/ByGroup/Edit/1等功能。这是因为我将有其他方式查看和编辑权限,如ByUser、ByActivity等 但是,我遇到了一个问题,即如何正确地路由到ByGroupController及其视图Admin/和Admin/Permissions/(从PermissionsController中激发)都可以正常工作 导航到管理员/
Admin/
我正试图在该区域内创建一个子目录:Admin/Permissions/ByGroup/
,具有Admin/Permissions/ByGroup/Edit/1
等功能。这是因为我将有其他方式查看和编辑权限,如ByUser、ByActivity等
但是,我遇到了一个问题,即如何正确地路由到ByGroupController
及其视图Admin/
和Admin/Permissions/
(从PermissionsController
中激发)都可以正常工作
导航到管理员/权限/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
ByGroupController.cs
除了简单的网站和没有那么多视图的网站,我没有真正使用过MVC。我在这方面做错了什么?有没有更好的方法来组织具有视图子目录的区域?我实际上不需要进行自定义路由,因此我的adminaregistration.cs
可能也不正确
非常感谢您的帮助和指导。默认情况下,路由只是一个抽象概念,也就是说,您的URL与控制器的物理位置完全无关。您可以使用路由以任意方式生成URL,同时将控制器放置在您想要的任何位置 另一方面,视图需要做一些额外的工作才能放入不遵循约定的不同目录中。您可以通过更改视图引擎来完成此操作-请参见
有一个第三方软件包,可以让您部分到达那里,它将根据您放置控制器的文件夹自动生成路由。但是,仍然需要您修改视图引擎来搜索视图,因为这些是完全独立的关注点。路由是针对传入请求的。视图的选择完全基于约定(尽管我可以被ViewEngine覆盖)。@ErikPhilips那么我如何深入到
权限
目录中,使用自己的控制器/视图访问其他目录呢?我想我已经理解了你的意思,@ErikPhilips。我现在保留了我的目录结构,但没有进行路由,而是返回视图,例如returnview(“~/Areas/Admin/Views/Permissions/ByGroup/Index.cshtml”)代码>来自myByGroupController
。这是正确的吗?
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" }
);
}
}
}
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();
}
}
}