使用JSON序列化将多个参数传递到REST服务

时间:2014-05-18 19:32:42

标签: c# json wcf rest

我正在尝试使用PUT方法将参数传递到我的RESTful服务中,但我总是收到400 HTTP错误(错误请求)。有人知道为什么会这样吗?

我的方法有下一步实现:

public interface IRestService
    {
        [WebInvoke(Method = "PUT", UriTemplate = "PutData", 
            RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, 
            BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        [OperationContract]
        bool PutData(User user, string userId);
    }


    [DataContract]
    public class User
    {
        [DataMember]
        public string firstname { get; set; }

        [DataMember]
        public string lastname { get; set; }
    }

要将这些数据传递给我的服务,我使用Fiddler和下一个参数:

url:http://localhost:28387/RestService.svc/PutData

正文:{"user":{"firstname":"John", "lastname":"Kennex"}, "userId":"15"}

我的web.config:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="SimpleRestService.SoapService" behaviorConfiguration="Default">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:28387/SoapService.svc"/>
          </baseAddresses>
        </host>
        <endpoint binding="basicHttpBinding" contract="SimpleRestService.ISoapService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
      <service name="SimpleRestService.RestService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:28387/RestService.svc"/>
          </baseAddresses>
        </host>
        <endpoint contract="SimpleRestService.IRestService" binding="webHttpBinding" behaviorConfiguration="restful">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restful">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="Default">
          <!--Чтобы избежать раскрытия метаданных, до развертывания задайте следующему параметру значение "false". -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- Чтобы при сбое получать подробные сведения об исключении для целей отладки, установите для нижеприведенного параметра значение true.  Перед развертыванием установите значение false, чтобы избежать раскрытия информации об исключении -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

0 个答案:

没有答案