ASP.NET Core Web API: why do I need a ModelState check in a Get request?

VS 2015 automatically generates the following code:

// GET: api/Companies/5
[HttpGet("{id}")]
public async Task<IActionResult> GetCompany([FromRoute] int id)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    Company company = await _context.Companies.SingleOrDefaultAsync(m => m.Id == id);

    if (company == null)
    {
        return NotFound();
    }

    return Ok(company);
}

What is the point of validating ModelState here?

+4
source share
1 answer

I assume you used the Web Api template. Since you did not start with the "Empty" template, the code generated by VS is what the Microsoft team considered "good practice."

, , , .., . , ( int, , , , ). . .

, .

0

Source: https://habr.com/ru/post/1667533/


All Articles