When moving a project into .Net Core, AddHeader
throws an error:
Error CS1061 'HttpResponse' does not contain a definition for 'AddHeader' and no extension method 'AddHeader' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?) .NETCoreApp,Version=v1.0
The simplest way you can add custom headers to every request response is by using a generic Middleware handler which uses app. Use() . app. Use((ctx, next) => { var headers = ctx.
Request headers are a great feature in ASP.NET Core that enable you to work with optional data represented as a collection of key-value pairs that can be transmitted back and forth between the client and server. The Request class provides access to metadata as well as headers of the HttpContext.
The AddHeader method adds a new HTML header and value to the response sent to the client. It does not replace an existing header of the same name. After a header has been added, it cannot be removed.
The answer is to instead do the following (without using AddHeader) :
Response.Headers["key-goes-here"] = "value-goes-here";
Checkout
Examples:
string combineValue = httpContext.Request.Headers["header1];
if (string.IsNullOrEmpty(combineValue)) // ...
var values = httpContext.Request.Headers["header1"];
if (StringValues.IsNullOrEmpty(values)) // ...
httpContext.Response.Headers["CustomHeader1"] = "singleValue";
httpContext.Response.Headers["CustomHeader2"] = new[] { "firstValue", "secondValue" };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With