How to define custom web.config in .Net Core 2 for IIS publish?

前端 未结 2 793
说谎
说谎 2021-01-19 05:40

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 l

相关标签:
2条回答
  • 2021-01-19 05:53

    not sure if you solved this, but if anyone else runs across this problem, I had this issue and finally went looking for the source code for the transform task. it contains some logging, so I ran the dotnet publish with a /v:n parameter, which sets the logging verbosity to "normal":

    dotnet publish src\MyProject -o ..\..\publish /v:n
    

    when I ran this, I saw this in the output:

    _TransformWebConfig:
      No web.config found. Creating 'C:\Development\MyProject\publish\web.config'
    

    even though there is a web.config in the project. I changed the properties of the web.config "Copy to Output Directory" to "Always", and now the web.config in my project gets merged with the auto-generated contents.

    my csproj now has this in it:

        <None Include="web.config">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    

    and the publish output has:

    _TransformWebConfig:
      Updating web.config at 'C:\Development\MyProject\publish\web.config'
    

    NOTE: if you are publishing to an existing directory that already has web.config in it, it will update that file. (i.e., an old publish). if you don't specify an output directory, it will publish to something like /bin/Debug/net471/publish/, which may have old files in it.

    NOTE2: you still need the Sdk="Microsoft.NET.Sdk.Web" attribute on the Project node in your csproj file, or it won't even bother looking for Web.configs.

    for reference, here is the task source code: https://github.com/aspnet/websdk/blob/master/src/Publish/Microsoft.NET.Sdk.Publish.Tasks/Tasks/TransformWebConfig.cs

    0 讨论(0)
  • 2021-01-19 06:04

    I finally got back to this and wound up using a transform:

    1. Create a web.release.config file in the root of the project

    2. Set that file's properties to Build Action = None so it doesn't get copied directly to the destination folder

    3. 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>
      
    0 讨论(0)
提交回复
热议问题