Restful WCF返回http 400

时间:2014-06-04 18:35:00

标签: c# wcf

我的网络服务界面看起来像

[ServiceContract]
    public interface IASchoolsWebService
    {

        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "Categories?shortString={shortString}")]
        IEnumerable<string> GetCategoriesByShortString(string shortString);
}

,实现类似于

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
    public class ASchoolsWebService: IASchoolsWebService
    {

public IEnumerable<string> GetCategoriesByShortString(string shortString)
        {
// my Implementation here 

}

}

使用Web引用可以很好地使用Web服务,但是我想使用RESTFULL服务来进一步了解应用程序,如果我使用浏览器调用了,为什么我会得到4oo

mywebservice.svc/Categories  

我使用web.conig,如:

<behaviors>
  <serviceBehaviors>
    <behavior name="SchoolsWebServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
    <behavior name="SchoolsWebServiceSecureBehavior">
      <serviceMetadata httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <service name="ArabicEWorld.WebService.ArabicEWorldWebService" behaviorConfiguration="SchoolsWebServiceBehavior">
    <endpoint name="UnsecureEndpoint" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfig" contract="ArabicEWorld.WebService.IArabicEWorldWebService" />
    <!--<endpoint name="SecureEndpoint"
             binding="basicHttpBinding"
             bindingConfiguration="secureBasicHttpBindingConfig"
             contract="MedStreaming.Schools.Server.WebService.IMedStreamingSchoolsWebService"/>-->
  </service>
</services>

2 个答案:

答案 0 :(得分:0)

Http错误代码400表示“BadData”,通常意味着您没有发送正确的参数。在您的代码中,您希望收到一个名为shortString的查询参数:

"Catagories?shortString={shortString}"

但是您没有在浏览器尝试中提供该参数:

mywebservice.svc/Catagories

如果您改为使用:

mywebservice.svc/Catagories?shortString=testString

您将发送正确的参数。

答案 1 :(得分:0)

您的代码与URL =&#34; http:// localhost:54632 / mywebservice.svc / Categories?shortString = shortString&#34;完美配合  但是如果你要返回其他类型而不是&#39; IEnumerable&lt;字符串&gt;&#39;那么您需要在接口中使用[ServiceKnownType] ..取决于您从方法返回的内容...

谢谢,