C
C#2mo 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
Hejle2mo ago
The Error from Mvc Looks like this for both Environment set to Development and Production
Hejle
Hejle2mo ago
No description
Hejle
Hejle2mo ago
The Error from MinimalApi Looks like this for Environment set to Development
Hejle
Hejle2mo ago
No description
Hejle
Hejle2mo ago
The Error from MinimalApi Looks like this for Environment set to Production
Hejle
Hejle2mo ago
No description
No description
Want results from more Discord servers?
Add your server
More Posts
How to get rid of nullable warning?What are the usual ways to get rid of these warnings? Do I have to check if everything is null beforyo im tied to make a game and there is problomtile class using System.Collections; using System.Collections.Generic; using TMPro; using UnityEnginNetArchTests Not Detecting Project Referencesanyone here familiar with netarchtests? I am trying to enforce clean architecture, and for some reasTrying to make a "Distributed SQLite" in C# and gRPC/Protobuf - don't know what to do nextHello, for a project that I am required to do, I need to make a simple distributed database system dUnable to get all the azure ad groups details which logged in user is a part of.I'm using Azure AD login for an app to login with Microsoft credentials and get the groups (and theiTesting my UserEdit Api endpoint using swagger fails and throws a code 400 responseI am trying to use Swagger to test my api endpoints but i get a code 400 when i test them. ``` [HttNative bindings to MacOS ObjC classes?It's very easy to have C bindings in C# on both Windows and Linux, but I'm really struggling to knowHow to get Windows.Storage.ProviderGood afternoon, I'm trying to use the namespace Windows.Storage.Provider (https://learn.microsoft.codependency-injection-dotnet-coreHello. I know that this is an old example but it is very important to me to run this code (If you waAvalonia: how to get the data of a control where the sender belongs to?My program requires a drag-and-drop. when i move a 'part' rectangle into another 'raw material' rect