C#C
C#2y ago
Hejle

MinimalApi vs Mvc - ValidationOfHeaders

I have been playing around with MinimalApi, and was surprised to see that MinimalApi could not handle validating Required HttpHeaders in the same way that MvcControllers can:

MinimalApi Code
app.MapGet("/weatherforecast", ([FromHeader]string TraceId) =>
{
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();


Mvc Code
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get([FromHeader]string TraceId)
{
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}


The Mvc Examples returns a ValidationProblemDetails-based error if TraceId while MinimalApi just retuns 400.
Does anyone know, how I can get MinimalApi to validate headers and send a meaningful error back (hopefully a ValidationProblemDetails).
Was this page helpful?