【问题标题】:Restful WCF return http 400Restful WCF 返回 http 400
【发布时间】:2014-06-06 16:25:20
【问题描述】:

我有一个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 

}

}

网络服务使用网络参考工作正常,但我想用作 RESTFULL 服务以进一步 mobuke 应用程序任何想法如果我使用浏览器调用我得到 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>

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

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

    "Catagories?shortString={shortString}"
    

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

    mywebservice.svc/Catagories
    

    如果您改为使用:

    mywebservice.svc/Catagories?shortString=testString
    

    您将发送正确的参数。

    【讨论】:

    • 即使添加参数我也有同样的问题
    【解决方案2】:

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

    谢谢,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2013-04-16
      相关资源
      最近更新 更多