c# - 是否有用于添加 HttpResponseHeader 的内容 Header Type?

标签 c# .net http-response-codes

我在 HttpResponseHeaders 中看到的唯一方法是 Add,它采用字符串类型作为 header 类型。 我只是想知道 .NET 是否提供了字符串中的 HttpResponseHeader 类型列表?

所以我可以这样做:

HttpResponseMessage response = Request.CreateResponse.........;
response.Headers.Add(xxxxx.ContentRange, "something");

我可以看到HttpResponseHeader中有一个Enum列表,但是它没有相应地提供字符串值...

即 HttpResponseHeader.ContentRange,但正确的 header 字符串应该是 Content-Range

如果我错了请纠正我...

最佳答案

System.Net.Http.Headers 命名空间中存在三个强类型 HTTP header 类:

HttpContentHeaders(可通过任何 System.Net.Http.HttpContent 类型的 Headers 属性访问)具有 Content-Type 的预定义属性,< em>Content-Length、Content-Encoding 等...(这似乎是您想要的 header )。

你可以这样设置它们:

var content = new StringContent("foo");
content.Headers.Expires = DateTime.Now.AddHours(4);
content.Headers.ContentType.MediaType = "text/plain";

...标题名称将被正确设置。

关于c# - 是否有用于添加 HttpResponseHeader 的内容 Header Type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15343872/

相关文章:

c# - 将大型光栅图形图像(位图、PNG、JPEG 等)转换为非矢量 postscript

c# - AutoMapper:运行时类型跳过给定成员

c# - SQL Server Compact 3.5 如何使应用程序自增 id?

c# - 如何使用我的 C# 应用程序触发 Windows 热键

Python:即使互联网关闭,响应代码也会返回 200?

php - 如何在 cakephp 2.x 中发送 410

c# - Roslyn c# CSharpCompilation - 编译动态

.net - Entity Framework : How to prevent overwrite "StoreGeneratedPattern" attribute on update model?

c# - .NET 4.5 应用程序在具有 .NET 4.0 的系统上的特定功能兼容性

azure - 调用 Azure 资源的 MOVE 时,204 响应意味着什么(而不是 202)