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)?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ingmar
  • 1,525
  • 6
  • 34
  • 51
  • What happens if you try to use `http://example.com/users?userId=7` (capitalized "I" in the "userId" name for query string parameter) – marc_s Nov 02 '22 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). – Ingmar Nov 02 '22 at 18:02

1 Answers1

1

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

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11