我希望我的 WCF 服务返回 json。
我在 ASP.NET 开发服务器上对其进行了测试,它可以从 WCF 测试客户端运行(方法调用可以)。
但是从浏览器测试失败并出现错误 400 错误请求。
web.config
:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WebServiceExample"
behaviorConfiguration="MetadataBehaviour">
<endpoint
address=""
behaviorConfiguration="AjaxBehaviour"
binding="webHttpBinding"
contract="WebService.IWebServiceExample">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="MetadataBehaviour">
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="AjaxBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
合同:
[ServiceContract]
public interface IWebServiceExample
{
[OperationContract]
[WebGet(UriTemplate = "/check/{key}",
ResponseFormat = WebMessageFormat.Json)]
DataElement Check(string key);
}
[DataContract]
public class DataElement
{
[DataMember]
public string Key { get; set; }
[DataMember]
public DateTime DateSince { get; set; }
[DataMember]
public DateTime DateTill { get; set; }
}
SVC 标记:
<%@ ServiceHost Language="C#" Debug="true"
Service="WebService.WebServiceExample"
CodeBehind="WebServiceExample.svc.cs" %>
我尝试的 URI 是:
http://localhost:16679/WebServiceExample.svc/check/asd