我有一个自托管的WCF /webHttpBinding端点绑定服务。我有几个不同内容类型的流,它提供服务。内容本身是正确传递的,但是任何OutgoingResponse.ContentType设置似乎都被忽略,而是每次都以"application/xml"的形式传递。
浏览器似乎克服了javascript和html (取决于如何使用它),而不是对更严格解释的css文件。CSS文件是我认识到这个问题的原因,但它对所有流来说都是一个问题。Chromebug和IE开发工具都显示"application/xml",不管我在内容类型的服务代码中放了什么。我还尝试在OutgoingResponse中将内容类型标头设置为OutgoingResponse,但这并没有什么区别,这可能只是OutgoingResponse.ContentType已经做了很长一段路。
[OperationBehavior]
System.IO.Stream IContentChannel.Code_js()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/javascript;charset=utf-8";
var ms = new System.IO.MemoryStream();
using (var sw = new System.IO.StreamWriter(ms, Encoding.UTF8, 512, true))
{
sw.Write(Resources.code_js);
sw.Flush();
}
ms.Position = 0;
return ms;
}添加了此行为:
var whb = new WebHttpBehavior
{
DefaultBodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.WrappedRequest,
DefaultOutgoingRequestFormat = System.ServiceModel.Web.WebMessageFormat.Json,
DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json,
HelpEnabled = false
};我试着设置AutomaticFormatSelectionEnabled = true和false,以防万一,因为它出现在谷歌的搜索中,但对此没有影响。
我找到了足够多的文章,它显示了Stream一起工作,把我搞糊涂了,为什么这不起作用。我相信Stream只是作为回应的主体,而不是整个信封。
我的.svclog没有显示出我认识到的任何有趣/相关的东西。
============
我可以在Fiddler2中确认头正在按照浏览器中所示的方式传递。
...
Content-Type: application/xml; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
...发布于 2013-07-15 14:29:18
解决了!
我在MessageInspector中有类似的内容:
responseProperty.Headers.Add("Access-Control-Allow-Origin",“*”;reply.Properties"httpResponse“= responseProperty;
这是在用reply.Properties覆盖已经存在的reply.Properties,包括任何contentType设置。相反,我尝试先得到HttpResponseMessageProperty,如果找到了,就使用现有的。
我很幸运地看到了那个。
https://stackoverflow.com/questions/17640347
复制相似问题