Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 839
Alex Avatar asked May 07 '18 22:05

Alex


People also ask

Where is the IIS Web config file located?

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.


1 Answers

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

like image 97
Dave Thieben Avatar answered Sep 20 '22 07:09

Dave Thieben