当我发布 .net 6 Web api 时,它会创建一个如下所示的 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>
<aspNetCore processPath="dotnet" arguments=".\MyDomain.MySubdomain.MyApp.Web.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>
然后,我需要进入该站点的 IIS-10,并在请求过滤下添加允许的动词 DELETE - 之后它看起来像这样:
<?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>
<aspNetCore processPath="dotnet" arguments=".\MyDomain.MySubdomain.MyApp.Web.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
<system.webServer>
<security>
<requestFiltering>
<verbs>
<add verb="DELETE" allowed="true" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
</configuration>
目前我必须记住每次更新应用程序时添加删除动词。
我的项目中不存在 web.config,它仅在我发布时创建。看来前任。 ASPNETCORE_ENVIRONMENT 行来自我的启动设置(我猜测),但我找不到类似的方法将请求过滤添加到 web.config。
启动设置:
"profiles": {
"MyDomain.MySubdomain.MyApp.Web": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7085;http://localhost:5085"
},
免责声明:我从 .Net 8 Publish configuration - Trying to add the IIS requestFiltering option to our pubxml
得到了这个答案将名为 web.config 文件的 XML 文件添加到您的项目中。仅包含添加动词的最低限度结构。当您发布项目时,这将与其他配置(例如环境变量)一起包含在输出中。
我在我的项目中使用它来明确允许
OPTIONS
动词。我建议在添加动词之前删除该动词,以防该动词已在服务器级别配置并由您的站点继承。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<verbs>
<remove verb="DELETE" />
<add verb="DELETE" allowed="true" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
</configuration>