HttpResponse 不包含 AddHeader for Dot Net Core 的定义

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

将项目移至 .Net Core 时,AddHeader 引发错误:

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

答案是改为执行以下操作(不使用 AddHeader):

Response.Headers["key-goes-here"] = "value-goes-here";

Checkout

示例:

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" };

或者你可以直接说:

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