Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web services RESTAPI和Post方法返回;“不允许使用方法”;错误_Web Services_Wcf_Rest_Http Post_Wcf Data Services - Fatal编程技术网

Web services RESTAPI和Post方法返回;“不允许使用方法”;错误

Web services RESTAPI和Post方法返回;“不允许使用方法”;错误,web-services,wcf,rest,http-post,wcf-data-services,Web Services,Wcf,Rest,Http Post,Wcf Data Services,我正在用Post方法在.net中创建一个RESTAPI,因为我想扩展从客户端获取数据的功能(注意,我不想使用GET方法) 下面是我的简单RESTAPI,它返回错误“methodnotallowed”。缺少什么 [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/T

我正在用Post方法在.net中创建一个RESTAPI,因为我想扩展从客户端获取数据的功能(注意,我不想使用GET方法)

下面是我的简单RESTAPI,它返回错误“methodnotallowed”。缺少什么

[OperationContract]
 [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test/{id}")]
        void Test(string id);
Url调用是
http://localhost/RestTestWebService.svc/json/Test/abs
。此调用返回方法不允许的错误

Web.config文件

<system.web>
    <compilation debug="true" targetFramework="4.0" />

    <!--<authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>-->

    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>




  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>

        <behavior name="RestTestWebServiceBehaviors">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>      
    </behaviors>

    <services>
      <service name="RestTest.RestTestWebService" behaviorConfiguration="RestTestWebServiceBehaviors" >
        <endpoint name="json" address="json" binding="webHttpBinding" behaviorConfiguration="WebHttpBehavior" contract="RestTest.IRestTestWebService"/>

        <!--<endpoint name="json" address="" binding="basicHttpBinding" contract="RestTest.IRestTestWebService"/>-->
      </service>
    </services>




    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"  />
  </system.serviceModel>
</configuration>

UriTemplate必须是“/Test”。您的运营合同应如下所示:

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test")]
 void Test(string id);
您的URL调用应该是:

var myTestUrl = 'http://localhost/RestTestWebService.svc/json/Test'
您应该在ajax调用的“数据”参数中发送“id”:

$.ajax({
    url: myTestUrl,
    contentType: "application/json; charset=utf-8",
    type: 'POST',       
    data: JSON.stringify({ 'id': 12345}),
    dataType: 'json'
}).done(function (response) {
    console.log('done');
});
(另外,服务端点上的
address=“json”
是不必要的,您只需拥有
address=“
和url=
http://localhost/RestTestWebService.svc/Test


更新在url中发送id并没有错误,我认为这与HTTP协议不同

谢谢你,这很有效。如果测试的webservice是Test(inti,字符串s),该怎么办。如果我使用DHC客户端发送请求,并将数据用作json字符串{“id”:1,“I”:1}。我还尝试了1,2作为DHC客户端的数据。更新了我的答案。另外,拥有一个整数参数对我也不起作用。我只发送数据契约(对象)或字符串,稍后将其转换为int。。。不太清楚为什么。。。是的,还请检查这些解决方案:另外,在通过邮件正文中的POST发送多个参数的情况下,请记住将
BodyStyle=WebMessageBodyStyle.WrappedRequest
添加到操作合同属性中。谢谢,这非常有帮助。我能够为我的Rest请求发送int数据,不需要从我的终端进行更改。任何关于休息的好书的建议。