Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid query string parameters in MVC

Tags:

asp.net-mvc

I have the following route

routes.MapRoute("CreateBook", "{controller}/{action}/{slug}/{name}", new { controller = "CreateBook", action = "Index" , slug = UrlParameter.Optional, name = UrlParameter.Optional});

For some reason, whenever I call RedirectToAction, the URL appears as

return RedirectToAction("Parameters", new { slug=1234, name="helloworld" });

http://localhost/CreateBook/Parameters?slug=1234?name=helloworld

What I would like is

http://localhost/CreateBook/Parameters/1234/helloworld

How do I achieve this?

like image 958
Razor Avatar asked Jul 02 '26 15:07

Razor


1 Answers

My guess is that the RedirectToAction call is picking up the default route, not your specialised route.

By default, when you pass in route values, MVC will append the values as querystring parameters.

Did you put that route before the default route?

like image 171
RPM1984 Avatar answered Jul 05 '26 17:07

RPM1984