简体   繁体   English

ASP.NET MVC - 嵌套路由/控制器

[英]ASP.NET MVC - Nesting Routes / Controllers

I have an ASP.NET MVC app. 我有一个ASP.NET MVC应用程序。 I have seen similar question asked. 我见过类似的问题。 However, I haven't found a good answer. 但是,我还没有找到一个好的答案。 Essentially, I want to use the following routes: 基本上,我想使用以下路由:

/admin/users
/admin/users/create
/admin/users/[someId]
/admin/roles
/admin/roles/create
/admin/roles/[someId]

I have the following file structure: 我有以下文件结构:

/Controllers
  AdminController.cs
  /Admin
    UsersController.cs
    RolesController.cs
/Views
  /Admin
    Index.cshtml
    /Users
      Index.cshtml
      Detail.cshtml
      Create.cshtml
    /Roles
      Index.cshtml
      Create.cshtml
      Detail.cshtml

When I run my app, I just get The resource cannot be found. 当我运行我的应用程序时,我只是得到了The resource cannot be found.

What am I doing wrong? 我究竟做错了什么? I set breakpoints, but none of them are being hit. 我设置了断点,但没有一个被击中。 It's like the routes aren't mapping to the controllers. 这就像路由没有映射到控制器。 I'm not sure what I need to do though. 我不确定我需要做什么。

You do not need to create sub folders for this to work. 您无需为此创建子文件夹。 Just have 2 controllers( UsersController and RolesController ) and you can use attribute routing to define the custom routing pattern you want. 只需要2个控制器( UsersControllerRolesController ),您就可以使用属性路由来定义所需的自定义路由模式。

Assuming you have attribute routing enabled 假设您已启用属性路由

public class UsersController : Controller
{
  [Route("admin/users")]
  public ActionResult Index()  { // to do : Return something }

  [Route("admin/users/create")]
  public ActionResult Create()  { // to do : Return something }

  [Route("admin/users/{id}")]
  public ActionResult View(int id)  { // to do : Return something }    
}

Or you can do the RoutePrefix on the controller level. 或者您可以在控制器级别上执行RoutePrefix

[RoutePrefix("admin/users")]
public class UsersController : Controller
{
  [Route("")]
  public ActionResult Index()  { // to do : Return something }

  [Route("create")]
  public ActionResult Create()  { // to do : Return something }

  [Route("{id}")]
  public ActionResult View(int id)  { // to do : Return something }    
}

You can do the samething for the RolesControllers as well. 你也可以为RolesControllers做同样的事情。

You can enable attribute routing in the RegisterRoutes method in RouteConfig.cs file. 您可以在RouteConfig.cs文件的RegisterRoutes方法中启用属性路由。

public static void RegisterRoutes(RouteCollection routes)
{            
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes(); //This line enables attribute routing 
   //Existing default Route definition goes here
}

You may also consider creating an "Admin" area and put your controllers inside that. 您也可以考虑创建一个“管理”区域并将控制器置于其中。 Areas are the right solution if you want to logically group similar functionality. 如果要对逻辑上的类似功能进行分组,则区域是正确的解决方案。

If you do not prefer attribute routing ( why not ?) , you an define these custom route patterns in your RouteConfig. 如果您不喜欢属性路由(为什么不呢?),您可以在RouteConfig中定义这些自定义路由模式。 The order in you define the route matters.So make sure you define your specific routes before the default generic one . 您定义路线的顺序很重要。因此,请确保在默认通用路线之前定义您的特定路线

You can also override your route tables by decorating your action methods with the RouteAttribute class. 您还可以通过使用RouteAttribute类修饰操作方法来覆盖路由表。

For example: 例如:

class AdminController
{
    [Route("/admin/users/create")]
    public ViewResult CreateUser()
    {
        ...
    }
}

This has the advantage of separating the method name from the url component. 这具有将方法名称与url组件分开的优点。

You can also route multiple URLs to a single method: 您还可以将多个URL路由到单个方法:

class AdminController
{
    [Route("/admin/users/{someId:guid}")]
    [Route("/admin/users/{someId:guid}/details")]
    public ViewResult UserDetails(Guid someID)
    {
        ...
    }
}

As mason said, the file structure isn't important in MVC routing. 正如梅森所说,文件结构在MVC路由中并不重要。

If you want to use convention (folder) based routing, you could use MvcCodeRouting to do exactly what you have specified here. 如果要使用基于约定(文件夹)的路由,可以使用MvcCodeRouting完成您在此处指定的操作。 It uses namespaces by default, so when you add controllers in a hierarchy, it will generate routes in the same hierarchy automatically. 它默认使用命名空间,因此当您在层次结构中添加控制器时,它将自动生成同一层次结构中的路由。 No need to apply the [Route] attribute everywhere and setup your routes manually. 无需在任何地方应用[Route]属性并手动设置路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM