I'm building a WCF REST Web service which should output JSON, but I'm getting XML.
I'm using ResponseFormat = WebMessageFormat.Json
Please help I search everywhere and I couldn't find the solution.
Note: I even tried the suggestion from here
config file
<services>
<service name="TestService">
<endpoint address=""
behaviorConfiguration="Tes开发者_开发问答tServiceAspNetAjaxBehavior"
binding="webHttpBinding"
bindingConfiguration="webBinding"
contract="TestService" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="Transport">
</security>
</binding>
</webHttpBinding>
</bindings>
Some things you need to check:
- The "name" attribute of your
<service>
element in web.config needs to match the fully-qualified name of the service class - ifTestService
is on namespaceMyNamespace
, then the service must be declared as<service name="MyNamespace.TestService">
- in other words, it must match the name you have in the '.svc' file for your service - Your endpoint declaration doesn't specify a
behaviorConfiguration
attribute; for WCF web endpoints, you need both to have the webHttpBinding and the a reference to a behavior which declares (in your case)<webHttp />
- Another option is to use the
WebServiceHostFactory
in the .svc file:<% @ServiceHost Service="MyNamespace.TestService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" Language="C#" debug="true" %>
. With this you don't need to have the service defined in config.
I had a similar issue because I had a tag like this;
<behavior name="jsonBehavior">
<enableWebScript/>
<webHttp helpEnabled="true"/>
</behavior>
after I remove
<webHttp helpEnabled="true"/>
it is fixed. Maybe it is a bug. I am not sure.
精彩评论