I'm working on a ASP.NET Web API. I'm trying to use a Put http call to update some resource.
I've been searching and there are a lot of possible configurations but I wasn't able to hit the right one for me.
I'm using the endpoints by convention. No routes on the Controllers, just the name of the function I want to execute.
public IHttpActionResult Put([FromBody]JObject data)
{
return this.ExecuteWithErrorHandling(() =>
{
return Ok();
});
}
The problem is that when I call this from my web-app is calling an OPTIONS first. And I get a 405 error.
So I've found that if I remove the OPTIONSVerbHandler and the ExtensionlessUrlHandler-Integrated-4.0 from the web config I can get a 200 on the options call.
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" /> <-- Remove this line -->
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> <-- Remove this line -->
</handlers>
But the problem is that after this when I call my Put function on the api, I get 404.
I'm using IIS express. So I went to the config file of IIS and add the missing verbs on the handlers section.
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
Also add the values on the web config to remove the WebDAV
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
and
<handlers>
<remove name="WebDAV" />
<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" />
</handlers>
Noticed that when I make a option call from postman I don't see the Put method.
Controller Full Code:
using Newtonsoft.Json.Linq;
using Rev.API.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Rev.API.Controllers
{
public class TestController : BaseApiController
{
public IHttpActionResult Get()
{
return this.ExecuteWithErrorHandling(() =>
{
int[] arr1 = new int[] { 3, 4, 5 }
return Ok(arr1);
});
}
public IHttpActionResult Get(string id)
{
return this.ExecuteWithErrorHandling(() =>
{
int Id = Convert.ToInt32(id);
return Ok(Id);
});
}
[System.Web.Http.HttpPut]
public IHttpActionResult Put([FromBody]JObject data)
{
return this.ExecuteWithErrorHandling(() =>
{
return Ok();
});
}
}
}
But the problem still happening. Not sure what I'm missing.