-2

How to validate if a parameter is null in asp .net core 2.1. In the below example the "IsValid" returns true.

This is the code we use

[HttpGet]
[Route("/api/test")]
public async Task<IActionResult> test([BindRequired, FromQuery]string id)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    return Ok();
}

TestUrl I use: testurl/api/test and Still ModelState.IsValid returns true

aumanjoa
  • 905
  • 1
  • 11
  • 30

1 Answers1

1

I don't see any problem here. If you are using "testurl/api/test?id=123", then your parameter is not null, in fact it's a perfectly valid integer (123), and therefore the ModelState.IsValid returns true.

Philzax
  • 196
  • 6
  • yes you are right, my bad. for testing I used the "testurl/api/tes"t and still ModelState.IsValid returns true – aumanjoa Aug 22 '18 at 05:12