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?
The configuration files for IIS 7 and later are located in your %WinDir%\System32\Inetsrv\Config folder, and the primary configuration files are: ApplicationHost. config - This configuration file stores the settings for all your Web sites and applications. Administration.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With