1

I am creating a REST API in .net with Post method as I want to extend getting data from the client ( Note I dont want to use GET method).

Here is my simple REST API which returns error " Method not allowed" . WHat is missing ??

[OperationContract]
 [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test/{id}")]
        void Test(string id);

Url call is http://localhost/RestTestWebService.svc/json/Test/abs. This call returns Method not allowed error.

Web.config file

<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>
Nuri YILMAZ
  • 4,291
  • 5
  • 37
  • 43
user476566
  • 1,319
  • 3
  • 26
  • 42

1 Answers1

1

UriTemplate must be "/Test". Your operation contract should look like:

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test")]
 void Test(string id);

Your URL call should be:

var myTestUrl = 'http://localhost/RestTestWebService.svc/json/Test'

You should send 'id' in the 'data' parameter of the ajax call:

$.ajax({
    url: myTestUrl,
    contentType: "application/json; charset=utf-8",
    type: 'POST',       
    data: JSON.stringify({ 'id': 12345}),
    dataType: 'json'
}).done(function (response) {
    console.log('done');
});

(Also, address="json" on service endpoint is not necessary, you can simply have adress="" and url = http://localhost/RestTestWebService.svc/Test)

UPDATE Sending the id in the url isn't incorrect, it is in my opinion not like the HTTP protocol

ralucarer
  • 146
  • 1
  • 1
  • 6
  • Thank you this works. What if the webservice for Test is Test(int i, string s) . If I use DHC Client to send the request and use the data as json string { "id":1, "i":1 } . I also tried 1,2 as data from DHC Client. – user476566 Jun 04 '14 at 14:41
  • Updated my answer above. Also, having an integer parameter doesn't work for me either. I send only datacontracts (objects) or string which i later on convert it to int ... not quite sure why ... yet – ralucarer Jun 04 '14 at 16:01
  • Check these solutions also: [link](http://stackoverflow.com/questions/545988/can-i-pass-non-string-to-wcf-restful-service-using-uritemplate) – ralucarer Jun 04 '14 at 16:04
  • 1
    And also remember to add `BodyStyle = WebMessageBodyStyle.WrappedRequest` to the operation contract attributes in the case of sending multiple parametrs via POST in the message body – ralucarer Jun 04 '14 at 16:16
  • Thank you this has been so helpful. I was able to send int data for my Rest request , no changed needed from my end. Any suggestions for a good book on REST. – user476566 Jun 04 '14 at 17:59
  • I'm glad ... I don't have any books :( just killing myself over the internet too – ralucarer Jun 05 '14 at 08:21