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.

2
  • What is type of userVM.UserId? If it is string then try like return RedirectToAction("details", "Users", new { userId = new Guid(userVM.UserId) });. Commented Apr 20, 2021 at 4:55
  • @Karan userVM.UserId is already GUID so no need to create Guid again. Sorry this one not work Commented Apr 20, 2021 at 5:11

1 Answer 1

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 });
    }
}     
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.