So I have a Self Hosted WEB API deployed on a remote server and whenever I try to call a POST method, I get a 405 response. In my research I have found that one needs to disable the WebDAV module, but that hasn't worked for me so far.
Here is my web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<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" />
</handlers>
<rewrite>
<rules>
<rule name="Redirects to www.domain.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^admin.domain.com$" />
</conditions>
<action type="Redirect" url="https://admin.domain.com/{R:0}" />
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
And this is my Main method that launched the WEB Api:
public static void Main(string[] args)
{
var cors = new EnableCorsAttribute("http://localhost:4300,https://admin.domain.com", "*", "GET,POST");
// Set up server configuration
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(_baseAddress);
config.MessageHandlers.Add(new CustomHeaderHandler());
config.EnableCors(cors);
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var server = new HttpSelfHostServer(config);
// Start listening
server.OpenAsync().Wait();
Console.WriteLine("Web API Self hosted on " + _baseAddress + " Hit ENTER to exit...");
Console.ReadLine();
server.CloseAsync().Wait();
}
The CustomHeaderHandler class is the following one:
public class CustomHeaderHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken)
.ContinueWith((task) =>
{
HttpResponseMessage response = task.Result;
response.Headers.Add("Allow", "GET, POST");
return response;
});
}
}
Whenever I attempt a POST, I receive the following response headers:
allow: GET, HEAD, OPTIONS, TRACE
content-length: 1293
content-type: text/html
date: Wed, 26 Jun 2019 11:12:14 GMT
server: Microsoft-IIS/10.0
status: 405
x-powered-by: ASP.NET
x-powered-by-plesk: PleskWin
I also tried the solution presented here: How to remove WebDAV module from IIS?
And I also tried this solution:
<modules>
<remove name="WebDAVModule"/>
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>
Does anyone know what the issue might be? The guys at the hosting place told me I should add a handler, but I am at a loss as to what I am doing wrong.
Thanks in advance.