HttpResponse does not contain a definition for AddHeader for Dot Net Core
Asked Answered
S

3

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

Siding answered 3/4, 2017 at 6:48 Comment(0)
S
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" };
Semester answered 3/4, 2017 at 11:20 Comment(2)
Your link is dead. check to make sure you entered it correctly.Gaiseric
Ah I see, good point, I am doing the combined values wrong, made it into single value only.Siding
S
9

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

Response.Headers["key-goes-here"] = "value-goes-here";
Siding answered 3/4, 2017 at 6:48 Comment(1)
thanks - "IHeaderDictionary has a different indexer contract than IDictionary, where it will return StringValues.Empty for missing entries."Faxon
S
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" };
Semester answered 3/4, 2017 at 11:20 Comment(2)
Your link is dead. check to make sure you entered it correctly.Gaiseric
Ah I see, good point, I am doing the combined values wrong, made it into single value only.Siding
K
1

Or you can just say:

Response.Headers.Add("key", "value");
Katherine answered 15/2, 2021 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.