我在wcf rest服务中遇到问题,返回json响应。在我的本地IIS-Webserver上运行Visual Studio 2010中的服务非常有效。
但是现在我在使用IIS 7.5的Windows Server 2008 R2上使用相同的web.config运行相同的服务,当我通过
调用服务时* HTTP://localhost/EchoService/EchoService.svc/echo/123
它不会返回json结果,而是一个带有未知文件类型的下载对话框,其中包含json结果。
首先我认为问题是,网络服务器不知道json mime类型所以我添加了它:
扩展程序: .json
MIME-类型: application / json
条目类型:本地
但它没有解决问题。你能否告诉我为什么它将结果作为文件返回而不知道文件类型?
这是我的web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="wcf_iis_proto_1.EchoService">
<endpoint address="" binding="webHttpBinding" contract="wcf_iis_proto_1.IEchoService" behaviorConfiguration="webEcho" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webEcho">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
这是我的服务合同(遗漏使用和命名空间):
public interface IEchoService
{
[OperationContract]
[WebGet(UriTemplate = "/echo/{message}", ResponseFormat = WebMessageFormat.Json)]
string EchoMessage(string message);
}
和服务实施:
public class EchoService: IEchoService
{
public string EchoMessage(string message)
{
return "Hey Buddy. You said: " + message + "!";
}
}
我希望你能帮助我。感谢!!!
答案 0 :(得分:0)
可能是您浏览器中的预期行为。它认为Json是一种下载类型。为避免这种情况,您应该更改内容类型以响应“text / plain”。 Here讨论了如何做到这一点。