【问题标题】:Plesk IIS POST not allowed不允许 Plesk IIS POST
【发布时间】:2019-06-26 11:27:52
【问题描述】:

所以我在远程服务器上部署了一个自托管 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>

有谁知道可能是什么问题?托管地点的人告诉我应该添加一个处理程序,但我不知道我做错了什么。

提前致谢。

【问题讨论】:

    标签: asp.net iis plesk iis-10


    【解决方案1】:

    我也有同样的问题。您正在寻找的 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 应用程序时一遍又一遍地添加此块。

    编辑:

    似乎在部署您的应用程序时,Web Deploy 将您的配置设置生成的 web.config 与您自己的自定义 web.config 文件结合在一起。因此,您只需将 web.config 文件放入您的 Web 项目中,例如:

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

    这会在部署到 IIS 后生成以下 web.config:

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

    【讨论】:

    • 谢谢,这真的很有帮助。我在 Plesk 上运行 ASP.NET core 3。
    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2022-01-23
    相关资源
    最近更新 更多