Samir Rawat
Samir Rawat

Reputation: 53

ActionLink to show parameters in URL instead of querystring mvc

I need this url http://localhost:53249/Admin/EditPosts/1/Edit but unfortunately i m getting query string in url like this http://localhost:53249/Admin/EditPosts?id=1&operation=Edit

This is my actionlink(anchor tag)

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

This is my route config:

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

        }

Upvotes: 0

Views: 572

Answers (1)

Guilherme Holtz
Guilherme Holtz

Reputation: 485

In this case you are mixing Action and Operation.

I suggest that you use the url like this:

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

Because this way you already indicate your action, Edit, for the Posts objects, and Edit is the action, not an operation, following standard REST.

To use the url suggested you must change the MapRoute to:

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

Upvotes: 1

Related Questions