不允许Plesk IIS POST

时间:2019-06-26 11:27:52

标签: asp.net iis plesk iis-10

因此,我在远程服务器上部署了自托管WEB API,每当我尝试调用POST方法时,都会收到405响应。在我的研究中,我发现需要禁用WebDAV模块,但是到目前为止,这对我没有用。

这是我的 web.config 文件:

<?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>

这是启动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();
}

CustomHeaderHandler 类如下:

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;
                });
    }
}

每当我尝试POST时,都会收到以下响应标头:

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

我还尝试了此处介绍的解决方案:How to remove WebDAV module from IIS?

我也尝试了以下解决方案:

<modules>
  <remove name="WebDAVModule"/>
</modules>
<handlers>
  <remove name="WebDAV" />
</handlers>

有人知道这个问题可能是什么吗?托管地点的家伙告诉我,我应该添加一个处理程序,但是我对自己做错了事感到茫然。

谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。您要查找的web.config修改是

<?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>

但是,.NET Core 3.0和3.1在项目中不带有web.config。 web.config在发布时生成。发布后,我手动添加了模块标签,但是每次发布Web应用程序时,都不能一遍又一遍地添加此代码块。