【问题标题】:ActionLink to show parameters in URL instead of querystring mvcActionLink 在 URL 中显示参数而不是查询字符串 mvc
【发布时间】:2016-11-25 09:31:45
【问题描述】:

我需要这个 url http://localhost:53249/Admin/EditPosts/1/Edit,但不幸的是,我在 url 中得到了这样的查询字符串 http://localhost:53249/Admin/EditPosts?id=1&operation=Edit

这是我的动作链接(锚标签)

<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 }
            );

        }

【问题讨论】:

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


    【解决方案1】:

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

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

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

    因为这样您已经为 Posts 对象指明了您的操作 Edit,而 Edit 是操作,而不是操作,遵循标准 REST。

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

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

    【讨论】:

    • 谢谢@guilherme,但在那之后我得到了这样的网址localhost:53249/Admin/Posts/1/Posts?operation=Edit
    • 尝试使用这样的操作链接:@Html.ActionLink(@posts.Title, "Edit", "Posts", new { id = posts.id }, null)
    • 但我有 2 个参数,第一个是 id,第二个是操作(编辑、详细信息)
    • 这就是我讲的重点,从概念上讲,你所说的操作实际上应该是一个动作,控制器中的方法
    • 操作是一个参数,用于以可编辑格式显示数据,该参数传递给现在重命名为“帖子”的操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多