(C# ASP.NET Core) [FromHeader] Parameter Binding of a Request Header

Parameters of a web api can be obtained from the values in a request header also. For this an attribute called "FromHeader" is used to decorate a parameter, and it provides an easy access to the value contained in the header of that name. In this tutorial we shall learn how to bind a header to a web api parameter. For continuity please go through the previous tutorial where we saw how to bind a query string variable to a parameter.
(Rev. 03-Aug-2022)

Categories | About |     |  

Parveen,

Open the Minimal WebApi Project

We already have a web api project that we created in the previous tutorial - where we bound a query string variable to a parameter of our web api. The completed project can be downloaded from the downloads attached to this and the previous video.

Open the solution explorer and locate the program.cs file. Double-click to open this file, and scroll to the part that you are seeing here.


// program.cs file 
// see the downloads for the completed file 

// relevant part . . . 

var app = builder.Build();

// route /{id}?m= 
app.MapGet("/{id}", (

    [FromRoute] int id,

    [FromQuery(Name = "m")] String? msg,

    [FromHeader(Name = "user-agent")] String? ua
    
    ) =>
{
    String qs = $"user-agent = {ua}";

    return Results.Ok(qs);
});

app.MapRazorPages();

app.Run();

Video Explanation (see it happen!)

Please watch the following youtube video:

Examine our sample web api

The GET Web Api that you are seeing here contains three parameters.

The first has been decorated as [FromRoute], and it is bound to the route segment called id.

The second parameter is bound to a query string variable, and we have discussed it completely in the previous tutorial.


app.MapGet("/{id}", (

    [FromRoute] int id,

    [FromQuery(Name = "m")] String? msg,

    [FromHeader(Name = "user-agent")] String? ua
    
    ) =>
{
    String qs = $"user-agent = {ua}";

    return Results.Ok(qs);
});

The third parameter is connected to a header value called User Agent. An http header is often used to pass additional information about how the resource has to be fetched, and also about the requesting client itself. It can contain information about authentication, caching, user agent, content negotiation headers like accept-encoding, accept-language, connection management headers like keep-alive, conditionals like etags, last-modified, if-match, and so on.

The parameter has been decorated as [FromHeader] - which means that it is derived from a header variable. The handler receives it through an identifier called ua, and echoes it back.

Run the Project

Run the project and type a GET request with a suitable url.

We observe that the result succeeds and the user agent value is echoed back without errors.

This is how we can bind a parameter to a header source. Thanks!


This Blog Post/Article "(C# ASP.NET Core) [FromHeader] Parameter Binding of a Request Header" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.