Cannot add [FromHeader] attribute for a model/class in ASP.NET Core 6.0 minimal api

Viewed 603

I'm trying to add multiple custom headers in the .NET core minimal API, so I added [FromHeader] attribute in front of input parameters.

It's working fine, I can see those headers in swagger.

But I'm looking for a better way to keep all these headers into a class/model and trying to add that in the API header by using [FromHeader] attribute in front of that model.

In this way, I'm getting this error

No public static bool method found for headers Testheader.tryparse(string,out TestHeaders) method found for

This is the code:

app.MapPost("api/v1/testapi",async([FromHeader] TestHeaderModel headers,{
    return Results.Ok(null)
})
.WithTags(new string[]{"test"})
.Produces<response>(200);

Model

public class TestHeaderModel
{
    [FromHeader]
    public string header1 {get;set;}
    [FromHeader]
    public string header2{get;set;}
}

My question: is there any way to keep all these headers in a single class/model and add [FromHeader] attribute for that particular class in the controller?

This approach is working in previous .net core versions, but not in 6.0, Is there any specific reason for that? or I'm missing anything here?

1 Answers
Related