MVC 4.0路由“区域”前缀

时间:2021-12-02 11:21:54

I Guess my question is very stuppied, but i could not find any awenser or link that worked fine, so.

我猜我的问题非常严重,但我找不到任何工作正常的awenser或链接,所以。

The point is, I want my url to reflect my folder strcuture. Witch is:

关键是,我希望我的网址反映我的文件夹strcuture。女巫是:

~/Controllers/Admin/ 
~/Controllers/Supply/

My routeconfig look like this:

我的routeconfig看起来像这样:

routes.MapRoute(
                 name: "Admin",
                 url: "Admin/{controller}/{action}",
                 namespaces: new [] {"Web_Intranet.Controllers.Admin"}
            );

routes.MapRoute(
                 name: "Supply",
                 url: "Supply/{controller}/{action}",
                 namespaces: new [] {"Web_Intranet.Controllers.Supply"}
            );

The problem is that if you type on the brower url the following:

问题是,如果您在浏览器URL上键入以下内容:

localhost:4342/admin/users/showall
OR
localhost:4342/supply/users/showall

Both will work! I assume that the routing checks wheter you pass a valid "controller" and "action" and chooses the first one that matches the condition.

两者都会奏效!我假设路由检查您是否通过了有效的“控制器”和“操作”,并选择匹配条件的第一个。

How can I create a valid routing config that will not work if the corresponding prefix doesn´t match?

如果相应的前缀不匹配,如何创建无效的路由配置?

1 个解决方案

#1


0  

Here's the configuration you should add for each Area;

这是您应为每个区域添加的配置;

~/Areas/Admin/AdminAreaRegistration.cs

using System.Web.Mvc;

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

    public override void RegisterArea(AreaRegistrationContext context)
    {
      context.MapRoute(
          "Admin_default",
          "Admin/{controller}/{action}/{id}",
          new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );
    }
  }
}

Make sure that Route_Name is unique

确保Route_Name是唯一的

#1


0  

Here's the configuration you should add for each Area;

这是您应为每个区域添加的配置;

~/Areas/Admin/AdminAreaRegistration.cs

using System.Web.Mvc;

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

    public override void RegisterArea(AreaRegistrationContext context)
    {
      context.MapRoute(
          "Admin_default",
          "Admin/{controller}/{action}/{id}",
          new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );
    }
  }
}

Make sure that Route_Name is unique

确保Route_Name是唯一的