2

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
2
  • 3
    It will generate ../team/details/1 if your using the default routing. Show your current route definitions and the signature of the Details() method Commented Feb 6, 2017 at 3:19
  • Thanks for that idea - I have a catch all route and I added it right before the default route and it is causing it to not recognize that pattern (controller/action/id). I switched it to add the default route first then the "catch all" route and it started working. Thanks! Commented Feb 10, 2017 at 16:34

1 Answer 1

3
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Turns out that my routes we're added on the wrong order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.