Back to Aspnetcore

MinApiEmptyStringInFormPost

aspnetcore/release-notes/aspnetcore-10/includes/MinApiEmptyStringInFormPost.md

latest850 B
Original Source

Treating empty string in form post as null for nullable value types

When using the [FromForm] attribute with a complex object in Minimal APIs, empty string values in a form post are now converted to null rather than causing a parse failure. This behavior matches the processing logic for form posts not associated with complex objects in Minimal APIs.

csharp
using Microsoft.AspNetCore.Http;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapPost("/todo", ([FromForm] Todo todo) => TypedResults.Ok(todo));

app.Run();

public class Todo
{
  public int Id { get; set; }
  public DateOnly? DueDate { get; set; } // Empty strings map to `null`
  public string Title { get; set; }
  public bool IsCompleted { get; set; }
}

Thanks to @nvmkpk for contributing this change!