0

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

        }

1 Answer 1

1

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
);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @guilherme but after that i am getting the url like this localhost:53249/Admin/Posts/1/Posts?operation=Edit
Try with action link like this: @Html.ActionLink(@posts.Title, "Edit", "Posts", new { id = posts.id }, null)
but i have 2 parameter first one is id and second one is operation(edit,details)
thats the point I talked about, by concept what you are calling operation in fact should be an action, the method in the controller
operation is a parameter which is use for showing data in editable format or not and this parameter pass into the action which is renamed now to "Posts"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.