Hardik Gondalia
Hardik Gondalia

Reputation: 3717

Show "ID" word in url in MVC

<a href="@Url.Action("UpdateCase", "Receptionist", new { id = @Model[SrNo].Patient_ID })" class="btn btn-danger">Edit</a>

In a href, I've pass id as parameter. so when I redirected to action I have following url

http://localhost:17888/Receptionist/UpdateCase/12

But I want id word visible in url.

http://localhost:17888/Receptionist/UpdateCase?id=12

RouteConfig

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

If I change parameter name rather than id, it is working. only id is not visible in url

UpdateCase Action:

[Authorize]
[HttpGet]
public ActionResult UpdateCase(Int64 id)
{

}

Upvotes: 4

Views: 490

Answers (2)

Roman Marusyk
Roman Marusyk

Reputation: 24579

One of possible solution is change your route config to

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

In that case you won't have a parameter with name id in the route table, so Url.Action can't use it and as a result you wil get expected url.

But be careful and check others actions

Upvotes: 3

Joseph Woodward
Joseph Woodward

Reputation: 9281

Question marks are added when the parameter is not specified in the URL, so removing the {id} from your route will force the question mark to be added in the URL.

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

Upvotes: 0

Related Questions