Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET 5中http头的所有类型都到哪里去了?_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# ASP.NET 5中http头的所有类型都到哪里去了?

C# ASP.NET 5中http头的所有类型都到哪里去了?,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,以前,在WebApi(在.NET 4.x上)中,我们可以通过类型化接口处理请求和响应的头(请参见HttpRequestMessage.headers/HttpResponseMessage.headers)。 现在,在ASP.NET 5中,我们有HttpRequest和HttpResponse,其Headers属性类型为IHeaderDictionary。但它只是一本非类型词典 下面我举了一个使用类型化访问的示例,它可以返回经过微调的http响应。需要创建一个HttpResponseMessag

以前,在WebApi(在.NET 4.x上)中,我们可以通过类型化接口处理请求和响应的头(请参见
HttpRequestMessage.headers
/
HttpResponseMessage.headers
)。 现在,在ASP.NET 5中,我们有
HttpRequest
HttpResponse
,其Headers属性类型为
IHeaderDictionary
。但它只是一本非类型词典

下面我举了一个使用类型化访问的示例,它可以返回经过微调的http响应。需要创建一个
HttpResponseMessage
并填充它的Headers集合(顺便说一句)

httpresponsemessageresponse=Request.CreateResponse(HttpStatusCode.OK);
response.Content=新的StringContent(manifestContent);
response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“文本/缓存清单”);
response.Headers.CacheControl=new CacheControlHeaderValue{NoCache=true,Public=true};
response.Headers.ETag=新的EntityTagHeaderValue(“\”+ETag+“\”);

在Asp.net 5中,headers集合现在是一个单独的类,即
HeaderDictionary
,可用于请求和响应头。这将作为标题的基于键值的存储。我能看到的好理由是因为Owin的支持。一个存储区可以跨各种Owin支持的中间件使用,例如WebApi、Signal,它为您提供了在标头集合中添加更多信息的扩展性。

如果您为Microsoft.AspNetCore.Http添加using语句,在
HttpRequest
HttpResponse
GetTypedHeaders
上有一些扩展方法,它们应该为您提供所需的类型安全性

在这个例子中,我还添加了
Microsoft.Net.Http.Headers
的using语句,只是为了清理它

var headers = Response.GetTypedHeaders();
headers.ContentType = new MediaTypeHeaderValue("text/cache-manifest");
headers.CacheControl = new CacheControlHeaderValue { NoCache = true, Public = true };
headers.ETag = new EntityTagHeaderValue("\"" + etag + "\"");

来源:

那么HttpContextBase呢?