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>

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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
Check these solutions also: link
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
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.