[英]Setting the Content-Type of an empty response in ASP.NET MVC
In order to support a legacy application that's in the field, I need my ASP.NET MVC app to return an empty response that also has a Content-Type
. 为了支持现场的遗留应用程序,我需要我的ASP.NET MVC应用程序返回一个也有
Content-Type
的空响应。 One of IIS, ASP.NET, or ASP.NET MVC is removing my Content-Type
when I send back a null response. 当我发回null响应时,IIS,ASP.NET或ASP.NET MVC之一正在删除我的
Content-Type
。 Is there any way around this? 有没有办法解决?
(While not requiring an empty response with a set Content-Type
would obviously be the ideal solution, the clients are already out there, and many of them cannot be upgraded.) (虽然不需要设置
Content-Type
的空响应显然是理想的解决方案,但客户已经在那里,其中许多都无法升级。)
EDIT : Since there was a request for code: I'm proxying the request from the new web application to the one that older clients rely on. 编辑 :由于存在代码请求:我正在将新Web应用程序的请求代理到旧客户端所依赖的请求。 To do this, I have a subclass of
ActionResult
, called LegacyResult
, that you can simply return for those methods that need to be handled by the old software. 为此,我有一个
ActionResult
的子类,名为LegacyResult
,您只需返回那些需要由旧软件处理的方法。 This is the relevant part of its code: 这是其代码的相关部分:
public override void ExecuteResult(ControllerContext context)
{
using (var legacyResponse = GetLegacyResponse(context))
{
var clientResponse = context.HttpContext.Response;
clientResponse.Buffer = false;
clientResponse.ContentType = legacyResponse.ContentType; /* Yes, I checked that legacyResponse.ContentType is never string.IsNullOrEmpty */
if (legacyResponse.ContentLength >= 0) clientResponse.AddHeader("Content-Length", legacyResponse.ContentLength.ToString());
var legacyInput = legacyResponse.GetResponseStream();
using (var clientOutput = clientResponse.OutputStream)
{
var rgb = new byte[32768];
int cb;
while ((cb = legacyInput.Read(rgb, 0, rgb.Length)) > 0)
{
clientOutput.Write(rgb, 0, cb);
}
clientOutput.Flush();
}
}
}
If legacyInput
has data, then Content-Type
is set appropriately. 如果
legacyInput
有数据,则适当地设置Content-Type
。 Otherwise, it's not. 否则,事实并非如此。 I can actually kluge the old backend to send an empty v. non-empty response for exactly the same request, and observe the difference in Fiddler.
我实际上可以克服旧的后端为完全相同的请求发送空的v。非空响应,并观察Fiddler的差异。
EDIT 2 : Poking around with Reflector reveals that, if headers have not been written at the time that HttpResponse.Flush
is called, then Flush
writes out the headers itself. 编辑2 :使用Reflector进行调查显示,如果在调用
HttpResponse.Flush
时尚未写入标题,则Flush
自行写出标题。 The problem is that it only writes out a tiny subset of the headers. 问题是它只写出一小部分标题。 One of the missing ones is
Content-Type
. 其中一个缺少的是
Content-Type
。 So it seems that, if I can force headers out to the stream, I can avoid this problem. 所以看来,如果我可以强制标题输出到流,我可以避免这个问题。
You have to trick the response into writing the headers, by falsely telling it there's content, then suppressing it : 你必须欺骗响应写入标题,错误地告诉它有内容,然后抑制它 :
/// [inside the writing block]
var didWrite = false;
while ((cb = legacyInput.Read(rgb, 0, rgb.Length)) > 0)
{
didWrite = true;
clientOutput.Write(rgb, 0, cb);
}
if (!didWrite)
{
// The stream needs a non-zero content length to write the correct headers, but...
clientResponse.AddHeader("Content-Length", "1");
// ...this actually writes a "Content-Length: 0" header with the other headers.
clientResponse.SuppressContent = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.