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

余生长醉 提交于 2019-12-10 15:48:38

问题


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.

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?


回答1:


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



来源:https://stackoverflow.com/questions/50223147/how-to-define-custom-web-config-in-net-core-2-for-iis-publish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!