user4655581
user4655581

Reputation:

Plesk IIS POST not allowed

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.

Upvotes: 0

Views: 1182

Answers (1)

Pieterjan
Pieterjan

Reputation: 3581

I'm having the same issue. The web.config modification you are looking for is

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <modules runAllManagedModulesForAllRequests="false">
        <remove name="WebDAVModule" />
      </modules>
      <aspNetCore ... />
    </system.webServer>
  </location>
</configuration>

However, .NET Core 3.0 and 3.1 do not come with a web.config in the project. The web.config is generated on publishing. I added modules tag manually after publishing, but I can't add this block over and over again each time I'm publishing my web application.

Edit:

It appears that when deploying your application, Web Deploy combines the web.config generated by your configuration settings along with your own, custom web.config file. So all you'd have to do is put the web.config file in your web project eg:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="false">
        <remove name="WebDAVModule" />
      </modules>
    </system.webServer>
  </location>
</configuration>

This produces the following web.config after deploying to IIS:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="false">
        <remove name="WebDAVModule" />
      </modules>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\MyProject.Web.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: -->

Upvotes: 1

Related Questions