【问题标题】:ASP.NET MVC 3 - Area URL Routing ProblemASP.NET MVC 3 - 区域 URL 路由问题
【发布时间】:2011-07-03 13:57:59
【问题描述】:

我有一个 ASP.Net MVC 3 应用程序。有 2 个区域:

  • Auth - 处理所有的身份验证等
  • 管理 - 物业管理领域

在管理区域中,我有一个 ManagementController 和一个 PropertyController。 ManagementController 什么都不做,它只有一个简单的 Index ActionResult 而不是返回一个视图。

PropertyController 有一个 Index ActionResult,它返回一个带有属性列表的视图,以及一个以 propertyId 作为参数的 Edit ActionResult。在 Property Index 视图中,我有一个带有属性列表的网格和一个使用以下代码编辑属性的选项:

@Html.ActionLink("Edit", "Edit","Property", new { id = item.PropertyId })

理论上这应该重定向到我的属性控制器的编辑操作结果,但是它重定向到我的管理控制器的索引操作结果。我的 ManagementAreaRegistration 文件如下所示:

context.MapRoute(null, "management", new { controller = "management", action = "Index" });
context.MapRoute(null, "management/properties", new { controller = "Property", action = "Index" });
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });

我的 global.asax 的 RegisterRoutes 是这样的:

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

我错过了什么,为什么它会重定向到错误的控制器和操作? 提前致谢!

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您可能需要在路由值参数中指定区域:

    @Html.ActionLink("Edit", "Edit", "Property", new { id = item.PropertyID, area = "Management" }, new { })
    

    根据用于此调用的构造函数,您需要指定最后一个参数 htmlAttributes,出于您的目的,该参数将为空。

    【讨论】:

    • 感谢您的输入,但我收到相同的结果,它仍然重定向到 ManagementController 的 Index 结果,而不是 PropertyController 的 Edit actionresult
    【解决方案2】:

    在您的路线中,您定义了一个名为 propertyId 而不是 id 的参数:

    context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });
    

    试试这个:

    @Html.ActionLink("Edit", "Edit","Property", new { propertyId = item.PropertyId })
    

    【讨论】:

    • 我已经测试了这条路线并且它有效:localhost/management/properties/edit/1
    • 好的,遵循这两个建议,例如指定区域,并将 Id 更改为 propertyId ...它有效!非常感谢!
    【解决方案3】:

    我建议使用约束。 例如我在 Global.asax 中的默认路由如下:

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

    然后在我的区域注册:

    context.MapRoute(
    "Search_default",
    "Search/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new string[] { typeof(MyProject.Areas.Search.Controllers.HomeController).Namespace }
                );
    

    这意味着我得到了 {AppName}/ 以及 {AppName}/Search/Home 并且两者都很有用。

    【讨论】:

      【解决方案4】:

      从任何区域到家做以下操作

      Url.Action("Details", "User", new { @id = entityId, area = "" })
      

      从家到任何地方

      Url.Action("Details", "Order", new { @id = entityId, area = "Admin" })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        相关资源
        最近更新 更多