【问题标题】:MVC4 routing 2 areas containing controllers with the same nameMVC4 路由 2 个包含同名控制器的区域
【发布时间】:2012-05-14 09:26:00
【问题描述】:

我在一个 mvc4 应用程序中有 2 个区域,并且我已经为每个区域注册了命名空间。

 public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Intergration_default",
            "Intergration/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
              constraints: null,
            namespaces: new[] { "WebApplication.Areas.Intergration.Controllers" }

        );

    } 
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Vend_default",
            "Vend/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional},
              constraints: null,
            namespaces: new[] { "WebApplication.Areas.MyController.Controllers" }

        );

我可以访问 Intergration/MyController 但是当我尝试访问 MyController 时出现错误

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

我做错了什么?我需要在 global.asax 中做一些额外的事情吗

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    根据您提供的代码/描述,听起来可能是几件事:

    1. 您在根控制器命名空间(即在项目根目录中的 Controllers 文件夹中,而不是在某个区域中)与另一个没有约束的区域存在控制器名称冲突。
    2. 更有可能的是,您对Vend 的第二个区域注册看起来像是一个不正确的命名空间。而不是WebApplication.Areas.MyController.Controllers,它应该是WebApplication.Areas.Vend.Controllers。我敢打赌,在你的根控制器命名空间中有一个控制器,它与 Vend 区域中的某些东西共享一个控制器名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      相关资源
      最近更新 更多