ActionLink在URL中显示参数而不是查询字符串mvc

时间:2016-07-21 16:57:43

标签: asp.net-mvc razor routing asp.net-mvc-routing asp.net-mvc-5

我需要此网址http://localhost:53249/Admin/EditPosts/1/Edit,但不幸的是我在网址中获取了查询字符串http://localhost:53249/Admin/EditPosts?id=1&operation=Edit

这是我的actionlink(锚标记)

<td>@Html.ActionLink(@posts.Title, "EditPosts", "Admin", new { id = posts.id, operation="Detail" }, null)</td>

这是我的路线配置:

 public static void RegisterRoutes(RouteCollection routes)
        {
            //routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes();
            routes.MapRoute(
             name: "Admin",
             url: "Admin/EditPosts/{id}/{operation}",
             defaults: new { controller = "Admin", action = "EditPosts"}
         );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        }

1 个答案:

答案 0 :(得分:1)

在这种情况下,您正在混合动作和操作。

我建议您使用这样的网址:

http://localhost:53249/Admin/Posts/1/Edit

因为这样你就已经为帖子对象指明了你的行动编辑,而编辑是行动,而不是操作,遵循标准REST。

要使用建议的网址,您必须将MapRoute更改为:

routes.MapRoute(
    name: "Admin",
    url: "Admin/Posts/{id}/{action}",
    defaults: new { controller = "Posts", action = "Details" } //Details action by default
);