Search code examples
asp.net-mvcasp.net-mvc-routingasp.net-routing

ASP MVC ReidrectToRoute without query string


I use this code in Controller to RedirectToRoute

return RedirectToRoute(new
            {
                controller = "Posts",
                action = "Post",
                parametr1 = "11",
                parametr2 = "aaaaaaaa"
            });

The url output is something like this:

https://localhost:45303/Posts/Post?parametr1=11&parametr2=aaaaaa

But I want url output doesn't has any query string like this:

https://localhost:45303/Posts/Post/1/aaaaaa

And this is my route :

  routes.MapRoute(
           name: "Default1",
           url: "{controller}/{action}/{parametr1}/{parametr2}/{parametr3}",
           defaults: new { controller = "Home", action = "Index", parametr1 = UrlParameter.Optional, parametr2 = UrlParameter.Optional, parametr3 = UrlParameter.Optional }
       );

How can do this? I use RedirectToRoute and RedirectToAction but result is same.


Solution

  • Use this overload for RedirectToRoute and provide proper route name:

    return RedirectToRoute("Default1", new
            {
                controller = "Posts",
                action = "Post",
                parametr1 = "11",
                parametr2 = "aaaaaaaa"
            });