1

in Asp Net Core, it seems that by default it's not enforce to check whether or not the model is valid. As such, I have to write something like this at the start of every controller:

if (!modelState.IsValid)
            {
                var errorText = "";
                foreach (var value in ModelState.Values)
                {
                    foreach (var error in value.Errors)
                    {
                        if (error.ErrorMessage != null)
                        {
                            errorText += error.ErrorMessage + "\n";
                        }
                        if (error.Exception.Message != null)
                        {
                            errorText += error.Exception.Message + "\n";
                        }
                    }

                }
                return StatusCode(400, errorText);
            }

Is there a way to automatically do this for every controller I decide ? I know there is [ApiController] tag but AFAIK it doesn't allow doing this much "logic" for the error message and return code

thanks

lezebulon
  • 7,607
  • 11
  • 42
  • 73

1 Answers1

1

Well, you actually do have the answer. [ApiController] does provide the behavior you're looking for, as documented.

The [ApiController] attribute makes model validation errors automatically trigger an HTTP 400 response.

Edit: I may have misunderstood what you mean by doing "this much logic". If you want to customize how ApiController responds, you might want to look into validation attributes. Furthermore, the documentation for ApiController mentions how it does the check with a filter, so you may find what you're looking for in that way too.

Ved
  • 301
  • 3
  • 6
  • I think that ValidationAttributes work in order to define the "validation" of the model, but in my case for instance, I want to surface to the user the potential ModelBinding errors that occured (eg, the model expected field X and the user didn't provide it) – lezebulon Aug 01 '22 at 22:26
  • In my experience, that is already the behavior of ApiController though. It doesn't simply return a 400, it lists what is wrong with the given model much like you have done. What you really can't customize is the exact layout of the message, like putting exception messages into it. If you're fine with letting ApiController define the layout of the error responded with, then validation attributes should cover you. You can even create custom attributes (with fully customized error messages) if that helps. – Ved Aug 01 '22 at 22:39
  • [Custom attributes](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#custom-attributes). The existing validation attributes allow you to define your own error messages too, but they're not as flexible since they're defined at compile time. – Ved Aug 01 '22 at 22:41
  • Some behaviour can be customised by `services.PostConfigure(options => {...});` https://stackoverflow.com/questions/51145243/how-do-i-customize-asp-net-core-model-binding-errors / https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-6.0#validation-failure-error-response – Jeremy Lakeman Aug 02 '22 at 02:00