8

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
goamn
  • 1,939
  • 2
  • 23
  • 39

3 Answers3

8

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

Response.Headers["key-goes-here"] = "value-goes-here";
goamn
  • 1,939
  • 2
  • 23
  • 39
  • thanks - "`IHeaderDictionary has a different indexer contract than IDictionary, where it will return StringValues.Empty for missing entries.`" – user326608 Nov 25 '17 at 16:42
3

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" };
McKabue
  • 2,076
  • 1
  • 19
  • 34
1

Or you can just say:

Response.Headers.Add("key", "value");