aspnet-core

ASP.NET Core - Handling Custom Header on Request/Response

by Abhith RajanApril 25, 2019 · 1 min read · Last Updated:

You can access the request headers or modify the response headers easily inside the controller in your ASP.NET Core applications.

Add custom header to response

1Response.Headers.Add("your-custom-header-id", custom_header_value);

Get a request header value

1var yourHeader = Request.Headers["your-custom-header-id"].FirstOrDefault();

Get bearer token value

Another example where we get the bearer token from the Authorization header,

1var authHeader = Request.Headers["Authorization"].FirstOrDefault();
2
3if (authHeader != null && authHeader.StartsWith("Bearer", StringComparison.OrdinalIgnoreCase))
4{
5 var loginToken = authHeader.Replace("Bearer ", string.Empty, StringComparison.OrdinalIgnoreCase);
6}

Written by Abhith Rajan
Abhith Rajan is an aspiring software engineer with more than 7 years of experience and proven successful track record of delivering technology-based products and services.
Buy me a coffee

Was this article helpful?

Your opinion matters

Please share your thought about this article

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Related Posts

Related Videos

Validation using MediatR's Pipeline Behaviors and FluentValidation | Clean .NET Core

Clean ASP.NET Core API using MediatR and CQRS | Setup

Clean Architecture with ASP.NET Core 3.0 - Jason Taylor - NDC Sydney 2019

Related Stories

Related Services

Flurl

Flurl is a modern, fluent, asynchronous, testable, portable, buzzword-laden URL builder and HTTP client library for .NET.