我的开发箱上有一个WebAPI服务,我试图从一个ASP.NET网站上调用,该网站也在我的开发箱上托管。 GET请求工作正常。但是,POST请求返回405.
我在这里提到的两个最常见的原因是IIS处理程序在WebAPI获取之前捕获请求,或者您没有为JavaScript正确安装Cors。这似乎都不适用于此,因为我已经检查了我的处理程序配置,而且我没有通过JavaScript进行服务调用。
从ASP.NET应用程序发出请求的代码是:
using (HttpClient client = new HttpClient())
{
Uri destination = new Uri("http://PromotionCodeProviderService/api/PromotionCode/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string newCodeJson = JsonConvert.SerializeObject(newCode);
HttpContent postContent = new ByteArrayContent(newCodeJson.ToRawByteArray());
HttpResponseMessage response = client.PostAsync(destination, postContent).Result;
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
return false;
}
}
在服务方面,路由是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "api/{controller}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
}
控制器代码是:
public class PromotionCodeController : ApiController
{
private readonly PromotionCodeService _promotionCodeService;
public PromotionCodeController()
{
_promotionCodeService = new PromotionCodeService();
}
// GET api/values
public List<PromotionCode> Get()
{
return _promotionCodeService.GetPromotionCodes();
}
// POST api/values
public void Post([FromBody]string value)
{
PromotionCode newCode = Json.Decode<PromotionCode>(value);
_promotionCodeService.AddPromotionCode(newCode);
}
...
如果我使用GET方法调用服务,我会进入上面的Get()方法,并且它会愉快地返回值。如果我使用POST方法调用服务,我会得到以下响应:
{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,PUT,POST,DELETE
Access-Control-Allow-Headers: Content-Type
Cache-Control: no-cache
Date: Fri, 05 Aug 2016 19:09:10 GMT
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 73
Content-Type: application/json; charset=utf-8
Expires: -1
}}
我的applicationHost.config中唯一不受特定文件路径限制的处理程序是:
<add name="TRACEVerbHandler" path="*" verb="TRACE" modules="ProtocolSupportModule" requireAccess="None" />
<add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="ProtocolSupportModule" requireAccess="None" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
我服务的web.config处理程序是:
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
如果我在我的服务的web.config中注释掉ExtensionlessUrlHandler-Integrated-4.0处理程序,则错误从405变为404,所以我很确定POST请求被路由到正确的处理程序 - 之后似乎事情变得糟糕了。
非常感谢任何可以提供帮助的人。