Mvc: Support validation and model binding attributes on action parameters

Created on 9 May 2016  路  5Comments  路  Source: aspnet/Mvc

c# [HttpGet] [Route("")] public async Task<IActionResult> Get([FromQuery(Name = "name")] String name) { if (!ModelState.IsValid) throw new Exception(); }

In the above snippet, if you call the action without any query string parameters the ModelState evaluates as valid and no exception is thrown.

Unfortunately, BindRequiredAttribute is not allowed on method parameters and there doesn't appear to be any other tools for marking a parameter as required. Of course, I can manually go through and null check everything, but I would much rather leverage ModelState.IsValid and HttpBadRequest(ModelState).

3 - Done enhancement

Most helpful comment

I think it would be nice to support validation attributes on parameters, as well as the attributes which affect model binding.

All 5 comments

@dougbu / @rynowak any reason not to allow [BindRequired] and friends to apply to parameters as well as properties/classes?

@Eilon if we changed the code so that attribute and its friends actually did something there, no.

I think it would be nice to support validation attributes on parameters, as well as the attributes which affect model binding.

Was this page helpful?
0 / 5 - 0 ratings