Restful WCF return http 400

本文关键字:http return WCF Restful | 更新日期: 2023-09-27 18:06:38

我有一个web服务接口,看起来像

[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服务为进一步的移动应用程序任何想法为什么我得到400如果我使用浏览器调用

mywebservice.svc/Categories  

我使用web。像这样:

<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>

Restful WCF return http 400

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

的查询参数。
"Catagories?shortString={shortString}"

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

mywebservice.svc/Catagories

如果你要用:

mywebservice.svc/Catagories?shortString=testString

你应该发送正确的参数

您的代码是工作完美的URL="http://localhost:54632/mywebservice.svc/Categories?shortString = shortString"但是如果你返回其他类型而不是IEnumerable <你需要在接口中使用[ServiceKnownType]。取决于你从你的方法返回什么…>

谢谢,