Reputation: 1349
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>
Upvotes: 1
Views: 5017
Reputation: 156
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
Upvotes: 1