C
C#8mo 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();
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();
}
[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).
6 Replies
Hejle
HejleOP8mo ago
The Error from Mvc Looks like this for both Environment set to Development and Production
Hejle
HejleOP8mo ago
No description
Hejle
HejleOP8mo ago
The Error from MinimalApi Looks like this for Environment set to Development
Hejle
HejleOP8mo ago
No description
Hejle
HejleOP8mo ago
The Error from MinimalApi Looks like this for Environment set to Production
Hejle
HejleOP8mo ago
No description
No description
Want results from more Discord servers?
Add your server