0

For my ASP.NET Core 6.0 MVC web application, I need both:

http://example.com/users/7 and
http://example.com/users?userid=7

My current controller looks like this:

    [HttpGet("users/{userId}")]
    public IActionResult GetUser(int userId)
    { ... }

The first call works, the second returns a 404.

I wonder why... and what do I need to do to fix this (allow both calls)?

2
  • What happens if you try to use http://example.com/users?userId=7 (capitalized "I" in the "userId" name for query string parameter) Commented Nov 2, 2022 at 17:45
  • Hey marc_s. Does not make a difference. I tried this already. As far as I know this is case insensitive - at least on IIS (and I run my app on a Windows server in IIS). Commented Nov 2, 2022 at 18:02

1 Answer 1

2

userId section is required so the second Url returned 404

You could try add ? to set Userid section nullable as below:

 [Route("Users/{UserId?}")]
            public IActionResult GetUser(int UserId)
            {
                return Ok();
            }

Result:

enter image description here

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

1 Comment

Works for me. And super video :) +Thank you Ruikai!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.