0

I have this code:

[Route("Users")]
public class UserRegistrationController : Controller 
{
    [HttpGet("details/{userId}")]
    public async Task<IActionResult> UserDetails(Guid userId)
    {
        // .....
    }

    [HttpPost("Save")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> SaveUser(UserVM userVM)
    {
        // ......
        return RedirectToAction("details", "Users", new { userId = userVM.UserId });
    }
}     

If I save the user redirectToAction generate userId as query string, this create an issue.

I mean instead of

https://localhost:5001/Users/Details/5de304c7-4c69-4819-c879-08d90306b555 

redirect to action creates the URL as

https://localhost:5001/Users/Details?userId=5de304c7-4c69-4819-c879-08d90306b555 

which causes a 404 error.

How do I solve this issue? I want to pass userId in route as below

https://localhost:5001/Users/Details/5de304c7-4c69-4819-c879-08d90306b555

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
leo
  • 451
  • 1
  • 3
  • 12
  • What is type of `userVM.UserId`? If it is `string` then try like `return RedirectToAction("details", "Users", new { userId = new Guid(userVM.UserId) });`. – Karan Apr 20 '21 at 04:55
  • @Karan userVM.UserId is already GUID so no need to create Guid again. Sorry this one not work – leo Apr 20 '21 at 05:11

1 Answers1

0

The issue was, the action method UserDetails need to add route [Route("details")] This will solve the issue.

[Route("Users")]
public class UserRegistrationController : Controller 
{
    [HttpGet("details/{userId}")]
    [Route("details")]
    public async Task<IActionResult> UserDetails(Guid userId)
    {
        // .....
    }

    [HttpPost("Save")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> SaveUser(UserVM userVM)
    {
        // ......
        return RedirectToAction("details", "Users", new { userId = userVM.UserId });
    }
}     
leo
  • 451
  • 1
  • 3
  • 12