Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpResponse does not contain a definition for AddHeader for Dot Net Core

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

like image 880
goamn Avatar asked Apr 03 '17 06:04

goamn


People also ask

How do I add a response header in .NET core?

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.

What is a header asp net?

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.

What is the use of response AddHeader?

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.


2 Answers

The answer is to instead do the following (without using AddHeader) :

Response.Headers["key-goes-here"] = "value-goes-here";
like image 194
goamn Avatar answered Sep 29 '22 01:09

goamn


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" };
like image 30
McKabue Avatar answered Sep 29 '22 03:09

McKabue