我正在从我的Angular.js客户端向ASP.NET Web API PUT方法发出以下请求:
var org = {
OrgId: 111,
name: 'testing testing'
};
$http.put("http://localhost:54822/api/data/putorganisation/1/", org).then(function(status) {
console.log("success PUT");
return status.data;
});
然而在 Fiddler 中出现以下错误消息:
{"message":"The requested resource does not support http method 'OPTIONS'."}
这是我的 asp.net web api web.config 文件的一部分:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type,x-xsrf-token,X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<remove name="WebDAV" />
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
数据控制器 Web API:
public HttpResponseMessage Options()
{
var response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
return response;
}
public HttpResponseMessage PutOrganisation(int id, [FromBody]Organisation org)
{
var opStatus = _Repository.UpdateOrganisation(org);
if (opStatus.Status)
{
return Request.CreateResponse<Organisation>(HttpStatusCode.Accepted, org);
}
return Request.CreateErrorResponse(HttpStatusCode.NotModified, opStatus.ExceptionMessage);
}
这是我的问题:当我在 Fiddler 中发出完全相同的请求(可行)和在 Angular 客户端中发出请求时为什么会收到错误消息(如上所示)?这是我的问题:当我在 Fiddler 中发出完全相同的请求(可行)和在 Angular 客户端中发出请求时为什么会收到错误消息(如上所示)?
<customHeaders> <add name="Access-Control-Allow-Credentials" value="true"/> <add name="Access-Control-Allow-Origin" value="http://appdomain.com" /> <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders>
- ShawnOPTIONSVerbHandler
和ExtensionlessUrlHandler-Integrated-4.0
,那么我会得到现有请求的404
错误。但是当我只注释掉OPTIONSVerbHandler
时,问题就解决了。 - Arsen Khachaturyan<!--<remove name="OPTIONSVerbHandler" />-->
就可以了。 - Aji