【问题标题】:How to use MapRoute, using ActionLink()?如何使用 MapRoute,使用 ActionLink()?
【发布时间】:2012-08-03 17:18:01
【问题描述】:

我在Global.asax 中有这个路由值

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

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

我使用这个 ActionLink 方法来调用 Edit 路由

@Html.ActionLink("Edit", "Topic", "Edit", new { id = item.ID })

现在生成的链接结果是这样的……

http://localhost:777/Admin/Topic?Length=4

如何使用 ActionLink 方法正确使用路由和目标。

谢谢!

【问题讨论】:

    标签: asp.net asp.net-mvc-3 routes target


    【解决方案1】:

    使用ActionLink 的正确重载来获得预期的结果

    @Html.ActionLink("Edit", "Topic", "Edit", new { id = item.ID }, null)
    

    重载是ActionLink(string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)

    当您向操作提供参数时,必须将null 添加为空 HTML 属性。或者,如果您确实需要将 HTML 属性应用于链接,您可以使用:

    @Html.ActionLink("Edit", "Topic", "Edit", new { id = item.ID }, new { @class = "MyCustomCssClassName" } )
    

    【讨论】:

    • Html.RouteLink() 更好。
    猜你喜欢
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多