Search code examples
c#asp.net-mvcasp.net-mvc-5asp.net-mvc-routingquery-string

MVC to redirect with route values instead of query string


I am working on a sample application where I have a register and detail actions. I want to redirect to the detail view after I registered a new team.

My Regsiter post code looks like this;

[HttpPost]
public async Task<ActionResult> Register(TeamEditorVm teamRegisterVm)
{
    if (ModelState.IsValid)
    {
        var teamDetailVm = await Managers.TeamManager.CreateAsync(teamRegisterVm);
        return RedirectToAction("Details", new { id = teamDetailVm.Id });
    }

    return View(teamRegisterVm);
}

Is there a way to force RedirectToAction("Details", new { id = teamDetailVm.Id }); to use route values for the URL like

hxxps://test.com/team/detail/1

instead of a querystring?

hxxps://test.com/team/detail?id=1

Solution

  • RedirectToAction("Details", new { id = teamDetailVm.Id });
    

    This will redirect you to Details action and if your routing contains id as the default parameter, then it will show you

    hxxps://test.com/team/detail/1
    

    and not as query parameter. I just tested it in MVC5 and it was working for me. Please check your routing.