【问题标题】:routing action within mvc4 area not able to map actionmvc4 区域内的路由操作无法映射操作
【发布时间】:2012-05-24 16:27:47
【问题描述】:

我在尝试让路线在我拥有的区域运行时遇到问题。

我的区域称为 ABC,并且我在该区域内有一个称为 Home 的控制器。如果我浏览“http://localhost:8000/abc”,我可以使用 Home/Index 打断点,但是当我尝试点击另一个名为“http://localhost:8000/ABC/details”之类的详细信息的操作时,我得到了404。

我试过了

context.MapRoute(
           "details",
           "ABC/Home/{action}/{id}",
           new { action = "details", id = UrlParameter.Optional },
             constraints: null,
           namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }

       );



        context.MapRoute(
          "ABC_Home",
          "ABC/{controller}/{action}/{id}",
          new { controller = "home",action="Index", id = UrlParameter.Optional },
            constraints: null,
            namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
      );

如果我使用“http://localhost:8000/ABC/Home/Details”,这允许我点击操作

 context.MapRoute(
           "details",
           "Home/Home/{action}/{id}",
           new {controller="home", action = "details", id = UrlParameter.Optional },
             constraints: null,
           namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }

       );

如果可能的话,理想情况下我不想在 URL 中使用 home。我究竟做错了什么?

任何帮助都会很棒!

【问题讨论】:

    标签: asp.net-mvc c#-4.0 asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    我不认为你可以默认一个带有变量动作名称的控制器,否则无法从路由中判断它是一个动作或控制器以及匹配哪个路由。我认为您可以对操作进行硬编码:

    Context.MapRoute(
        "ABC_Home_Details",
        "ABC/Details/{id}",
        new { controller = "home", action="details", id = UrlParameter.Optionsl },
        constraints: null,
        namespaces: new [] { "WebApplication.Areas.ABC.Controllers" }
    );
    

    【讨论】:

      【解决方案2】:

      我认为您只需要一条路线即可。不要在路由中包含控制器,因为它似乎以 /ABC 开头;只需将控制器分配为默认值:

      context.MapRoute(
          "ABC_Home",
          "ABC/{action}/{id}",
          new { controller = "home", action="Index", id = UrlParameter.Optional },
          constraints: null,
          namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
      }
      

      根据您的要求,这会将 /abc 路由到 /home/index,并将 /abc/details 路由到 /home/details.

      然后,如果您需要访问其他控制器,您可以为此添加另一个规则,类似于默认规则:

      context.MapRoute(
          "Default_Route",
          "{controller}/{action}/{id}",
          new { id = UrlParameter.Optional }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-05
        • 1970-01-01
        相关资源
        最近更新 更多