Headers, what we can do
- Add custom response header
- Add custom request header
- Get request header value
- Get response header value
- Get list of request headers
- Get list of response headers etc.
Practical Examples
- Add a custom response header in action method using HttpContext
- Add a custom response header globally to all the responses using middleware in Startup class
- Add a custom response header in more than one action methods using ResultFilterAttribute class
- Add a custom response header in Index view using RazorPage.Context
- List all response headers in Index view using RazorPage.Context
- Get a request header value, for example, of Authorization header and then its bearer token value
Add a custom response header In action method
Case1
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace WebAppMvc1.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
HttpContext.Response.Headers["blogger"] = "appliedk";
return View();
}
}
}
Case2
app.UseStaticFiles();
app.Use(async (context, next) =>
{
context.Response.Headers.Add("developer", "shri");
await next();
});
app.UseRouting();
Output in Chrome Developer for case1 and case2
Case3
Step1. Add class AddResponseHeaderAttribute : ResultFilterAttribute class in Filters folder. Note that it inherits ResultFilterAttribute class. Override the OnResultExecuting method. Write the logic of adding response header in it.
using Microsoft.AspNetCore.Mvc.Filters;
namespace WebAppMvc1.Filters
{
public class AddResponseHeaderAttribute : ResultFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
context.HttpContext.Response.Headers.Add("country", "India");
base.OnResultExecuting(context);
}
}
}
Step2. In Home controller, create Get action. Apply the custom attribute AddResponseHeader on this Get action.
The advantage of attribute based header is that we can apply the attribute on all the actions where we need the header addition. The logic to add the header is in one place in the class which overrides the ResultFilterAttribute.
[AddResponseHeader]
public IActionResult Get()
{
// header will be added beause of AddResponseHeader attribute
return View();
}
Case4
use RazorPage.Context as shown in the below code in Index view.
@{
Context.Response.Headers.Add("ajeet","appliedk.blogspot.com");
}
Case5
@{
ViewData["Title"] = "Home Page";
var headers = Context.Request.Headers;
var keys = headers.Keys;
Context.Response.Headers.Add("ajeet", "appliedk.blogspot.com");
}
<h3>List of all @headers.Count Request headers</h3>
<table class="table table-striped">
@if (true)
{
foreach (var key in keys)
{
<tr>
<td>@key</td>
<td>@headers[key]</td>
</tr>
}
}
</table>
<h2>Using razorPage.Context to get HttpContext</h2>
<h3>@Context.Connection.LocalIpAddress.ToString()</h3>
<h3>@Context.Connection.LocalPort</h3>
<h3>@Context.Request.ContentType</h3>
<h3>@Context.Request.Host.Host</h3>
Case6 Get Bearer Token Based on Authentication Request Header
var authHeader = Request.Headers["Authorization"].FirstOrDefault();
if (authHeader != null && authHeader.StartsWith("Bearer", StringComparison.OrdinalIgnoreCase))
{
var loginToken = authHeader.Replace("Bearer ", string.Empty, StringComparison.OrdinalIgnoreCase);
}
No comments:
Post a Comment