LoginSignup
1
2

More than 3 years have passed since last update.

ASP.NET Core + IIS 10 でWebAPIのDELETEがNOT ALLOWで405の件

Posted at

IIS 10でDELETEやPUTが405

ある日突然、DELETEメソッドが405エラーを吐くようになっちまいました。method not allowed!
過去からの情報が出回っているとおり、DELETEとPUTのverbがASP.NET CoreのハンドラではなくWebDavのハンドラで処理される設定になってしまっていることに起因しているようでした。対策のアプローチも「WebDavなんか使わねえから無効化してしまえ」から変わりませんが、IIS 10(Windows Server 2016)においては設定箇所や設定の実装が違う?ようで、超ドはまりしたのでメモしておきます。

エラー詳細

request: DELETE /api/hoge
response:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>405 - HTTP verb used to access this page is not allowed.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>405 - HTTP verb used to access this page is not allowed.</h2>
  <h3>The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access.</h3>
 </fieldset></div>
</div>
</body>
</html>

というかんじ。Response Headerには↓がAllowだよ、という返事が。
image.png

解決策、失敗編

Web.configのsystem.webServerの中に<remove name="WebDav" />を追記!などの記事が散見されますが、そのとおりにやってもDELETEは405のままだったり、そもそもWeb.configの文法エラーになったり・・。とにかく、結果、うまくいきませんでした。
ちなみに ASP.NET Core 3.0 でデフォルト?のweb.configはこんな感じです。
system.webServerのディレクティブの中にどう書いたら正しいのか、、結局よくわからん。

<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\hogehoge.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

解決策、成功編

結果、configベースでなくIISの設定マネージャでWebDavハンドラを削除したらDELETEも使えるようになりました!
image.png
「モジュール」を選択し、その中のWebDAVModuleを削除。
image.png

なお削除の下に親構成に戻すで復活しますので、 試しにやってみる ことができます。

やってることはDELETEなどのverbにWebDAVModuleが反応しちゃうのを無効にするだけなので従来のweb.configやapplicationhost.configをいじる方法と変わらないんだとは思いますが、なにせconfigの正しい修正の在り方が変わりまくってる気がして、過去の情報が通用しません。こーゆーの増えてきたね。
何はともあれ、一件落着!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2