如何在 .Net Core 2 中为 IIS 发布定义自定义 web.config? [英] How to define custom web.config in .Net Core 2 for IIS publish?
问题描述
VS 将生成(并覆盖)一个 web.config 文件作为发布到 IIS 的一部分.我需要在文件中包含各种项目(例如扩展文件上传最大大小限制、重定向到 HTTPS 等).我目前必须在每次发布后将内容复制并粘贴到文件中.
VS will generate (and override) a web.config file as part of publishing to IIS. I have various items I need to include in the file (like extending the file upload max size limit, redirecting to HTTPS, etc.). I am currently having to copy and paste the contents into the file after every publish.
有没有办法定义 Visual Studio 在为 IIS 发布 .NET Core 2 Web 应用时生成的 web.config 文件的内容?
Is there a way to define the contents of the web.config file that Visual Studio generates when publishing a .NET Core 2 web app for IIS?
推荐答案
我终于回到了这个问题并使用了一个转换:
I finally got back to this and wound up using a transform:
在项目根目录下创建 web.release.config 文件
Create a web.release.config file in the root of the project
将该文件的属性设置为 Build Action = None,这样它就不会被直接复制到目标文件夹
Set that file's properties to Build Action = None so it doesn't get copied directly to the destination folder
使用转换语法定义需要插入的部分:
Use the transformation syntax to define the sections that need to be inserted:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location>
<system.webServer>
<security xdt:Transform="Insert">
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200" />
</requestFiltering>
</security>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<modules xdt:Transform="Insert" runAllManagedModulesForAllRequests="false">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
</location>
</configuration>
这篇关于如何在 .Net Core 2 中为 IIS 发布定义自定义 web.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!