服务器响应中缺少 ContentType HTTP 标头
我有一个 ASP.NET MVC Web 操作,它返回一个简单的 zip 文件。 Responce.ContentType 属性手动设置为“text/xml; charset=utf-8; gzip”。该标头值是在将响应内容写入输出流之前设置的。 Web 项目托管在 Windows Azure 托管上。问题是有时服务器返回的响应缺少 ContentType 标头字段,这会导致客户端出现问题。不知道这可能是什么原因。当我在本地运行相同的 Web 项目时 - 一切正常,ContentType 字段具有正确的值。示例网络操作代码:
public void GetData()
{
Response.ContentType = "text/xml; charset=utf-8; gzip";
XDocument xml = new XDocument(...);//some large XML file
byte[] byteData = Encoding.UTF8.GetBytes(xml.ToString());
Stream outputStream = Response.OutputStream;
GZipStream compressedzipStream = new GZipStream(outputStream, CompressionMode.Compress);
compressedzipStream.Write(byteData, 0, byteData.Length);
compressedzipStream.Close();
}
任何帮助将不胜感激。
I have a ASP.NET MVC web action which returns a simple zip file. The Responce.ContentType property is manually set to "text/xml; charset=utf-8; gzip". This header value is set before writing response content to the output stream. Web project is hosted on Windows Azure hosting. The problem is that sometimes server returns response with missing ContentType header field, this causes issues on the client side. Having no idea what could be the reason of it. When I run same web project locally - everything works fine, ContentType field has proper value. Sample web action code:
public void GetData()
{
Response.ContentType = "text/xml; charset=utf-8; gzip";
XDocument xml = new XDocument(...);//some large XML file
byte[] byteData = Encoding.UTF8.GetBytes(xml.ToString());
Stream outputStream = Response.OutputStream;
GZipStream compressedzipStream = new GZipStream(outputStream, CompressionMode.Compress);
compressedzipStream.Write(byteData, 0, byteData.Length);
compressedzipStream.Close();
}
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写自定义操作结果:
然后:
另请注意
text/xml;字符集=utf-8; gzip
不是标准的 HTTPContent-Type
标头。因此,除非您编写一些能够理解它的自定义客户端,否则任何标准浏览器都不太可能能够解析它。如果您想表明响应已压缩,您最好使用 内容编码 标头。您可以直接在 IIS 级别激活动态内容压缩,而不是打扰您的代码,或者如果您无法访问 IIS,您可以简单地编写一个自定义操作过滤器:
You could write a custom action result:
and then:
Also note that
text/xml; charset=utf-8; gzip
is not a standard HTTPContent-Type
header. So unless you write some custom client that will understand it, it is unlikely that any standard browser will be able to parse it.If you wanted to indicate that the response is compressed you'd better use the Content-Encoding header. You could either activate compression for dynamic contents directly at IIS level and don't bother in your code or if you don't have access to IIS you could simply write a custom action filter:
试试这个:
try this: