I am defining my action link like this:
@Html.ActionLink("Visit profile By Name", "UserProfile", "User", new { UserId = 2, UserName = "Sandeep" }, null)
Which generates the link:
http://localhost:2010/User/UserProfile/Sandeep/2
If username == string.empty (as username is coming dynamically), then the link becomes
http://localhost:2010/User/UserProfile?UserId=2
But in that case I want my link like to look like this:
http://localhost:2010/User/UserProfile/2
Route table value:
routes.MapRoute(
"UserName", // Route name
"User/UserProfile/{UserName}/{UserId}", // URL with parameters
new { controller = "User", action = "UserProfile", UserName = UrlParameter.Optional, UserId = UrlParameter.Optional } // Parameter defaults
);
change your route
routes.MapRoute( "UserName", // Route name
"User/UserProfile/{UserName}/{UserId}", // URL with parameters
new {
controller = "User",
action = "UserProfile",
UserName = "UserProfile", /*change this */
UserId = UrlParameter.Optional
} // Parameter defaults
);
change UserName = UrlParameter.Optional to UserName = "UserProfile"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With