添加管理控制器的路由

发布于 2024-12-20 15:28:32 字数 636 浏览 1 评论 0原文

我已将所有管理控制器放入 Controller 文件夹中的 Admin 文件夹中。由于我的一个管理控制器与 Controller 文件夹中另一个控制器的名称匹配,因此我收到以下错误。

发现多个类型与名为“Product”的控制器匹配。如果服务此请求的路由 ('{controller}/{action}/{id}') 未指定命名空间来搜索与请求匹配的控制器,则可能会发生这种情况。如果是这种情况,请通过调用采用“namespaces”参数的“MapRoute”方法的重载来注册此路由。

我尝试添加以下路由,但问题仍然相同

routes.MapRoute(
    "", //Route name 
    "Admin/{controller}/{action}/{id}", // Url with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

我该如何摆脱这个错误。更改控制器的名称是我心中的最后一个选择。现在,我正在寻找一种保留名称的方法,看看是否可以找到另一种方法来解决这个问题。

I have placed all my admin controllers inside an Admin folder in Controller folder. Since one of my admin controller matches the name of another controller in Controller folder, I am getting the following error.

Multiple types were found that match the controller named 'Product'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

I tried adding the following route, but still the problem is same

routes.MapRoute(
    "", //Route name 
    "Admin/{controller}/{action}/{id}", // Url with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

How can i get rid of this error. Changing the name of controller is the last option on my mind. Right now, I am looking for a way to preserve the name and see if i can find another way around this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-27 15:28:32

我已将所有管理控制器放置在 Controller 文件夹中的 Admin 文件夹中

,这就是您的问题。在给定以下请求 /admin/index 的情况下,您如何期望默认控制器工厂知道您想要实例化哪个控制器(Controllers 文件夹中的控制器或 Controllers/Admin 文件夹中的控制器)?请记住,默认控制器工厂会在从 Controller 派生的加载程序集中搜索类型。它并不真正关心它们在哪个文件夹中声明。因此,当它发现您有 2 个同名控制器时,它不知道该选择哪一个。

一种可能性是使用区域。然后,您可以在注册路由时指定名称空间:

routes.MapRoute(
    "", 
    "Admin/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "AppName.Areas.Admin.Controllers" }
);

同样在 Global.asax 中,确保为非区域控制器指定名称空间:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "AppName.Controllers" }
);

I have placed all my admin controllers inside an Admin folder in Controller folder

Well, that's your problem. How do you expect the default controller factory to know which controller you want to be instantiated given the following request /admin/index (the one in the Controllers folder or the on in the Controllers/Admin folder)? Remember that the default controller factory searches for types in the loaded assemblies that derive from Controller. It doesn't really care in which folder they were declared. So when it finds that you have 2 controllers with the same name it doesn't know which one to pick.

One possibility is to use Areas. Then you could specify namespaces when registering the route:

routes.MapRoute(
    "", 
    "Admin/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "AppName.Areas.Admin.Controllers" }
);

Also in your Global.asax make sure that you specify the namespace for the non-area controllers:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "AppName.Controllers" }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文