Error Reporting for Minimal APIs Form Binding
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
When a Minimal API handler parameter is bound to form values using `FromFormAttribute`, and the form data contains invalid data that cannot be bound (for example an 'abc' value that cannot be bound to an int property), the binding fails completely. And the only way to get information about the invalid input is inspecting the `BadHttpRequestException`. We have no way of generating an error message for the user that contains all the binding errors + any other validation errors.
Compare this to the old Controllers and Razor Pages behavior. When a binding error occurs, we have a `ModelState` with the validation errors. And the action/handler method still executes so we can handle the invalid input however we like.
### Describe the solution you'd like
I understand that Minimal APIs do not have a `ModelState`. And, to me, parameter binding is not very important. I am totally fine with calling a function inside the handler to parse and validate the model. So I wish we had such a function
```csharp
public static class ModelParserValidator
{
public static ParsingAndValidationResult ParseAndValidate(IFormCollection requestForm)
{ }
}
public class ParsingAndValidationResult
{
public bool IsValid { get; }
public TModel? Model { get; }
public IEnumerable ValidationErrors { get; }
}
```
Then the `ParseAndValidate` function can be used inside the minimal API handler like so:
```csharp
app.MapPost("/", (HttpRequest httpRequest) =>
{
ParsingAndValidationResult result = ModelParserValidator.ParseAndValidate(httpRequest.Form);
// Then the developer handles the valid/invalid input however they like.
});
```
### Additional context
The reason I am asking for this feature is that I am trying to use Minimal APIs to build an SSR web application. And I am trying to handle forms that will be submitted by human users from a browser. I know that this is not what Minimal APIs are for, but my options are limited because I am trying to avoid Blazor SSR which is unnecessarily complex, and trying to avoid MVC and Razor Pages which are falling out of favor.
Contributor guide
Research direction
Start by tracing Minimal API parameter binding for FromFormAttribute and the handler path described in the issue. Compare its handling of invalid form values with the existing Controllers and Razor Pages ModelState behavior. Done means the Minimal API can expose all binding and validation errors so a handler can decide how to respond.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100