我正在使用Post方法在.net中创建REST API,因为我想扩展从客户端获取数据(注意我不想使用GET方法)。
这是我的简单REST API,它返回错误"方法不允许" 。缺少什么?
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test/{id}")]
void Test(string id);
网址通话为http://localhost/RestTestWebService.svc/json/Test/abs
。此调用返回Method not allowed错误。
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>
答案 0 :(得分:0)
UriTemplate必须“/ Test”。您的运营合同应如下所示:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test")]
void Test(string id);
您的网址应该是:
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"
不是必需的,您只需adress=""
和url = http://localhost/RestTestWebService.svc/Test
)
更新在网址中发送ID不正确,我认为不喜欢 HTTP协议