如何在IIS发布的.Net Core 2中定义自定义web.config? [英] How to define custom web.config in .Net Core 2 for IIS publish?

查看:114
本文介绍了如何在IIS发布的.Net Core 2中定义自定义web.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS将在发布到IIS时生成(并覆盖)一个web.config文件.我需要在文件中添加各种项目(例如,扩展文件上载的最大大小限制,重定向到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:

  1. 在项目的根目录中创建一个web.release.config文件

  1. 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>

这篇关于如何在IIS发布的.Net Core 2中定义自定义web.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆