We learned how to publish an ASP.NET Core web application to IIS in the preceding article. As we’ve seen, the machine where the application will be hosted must have ASP.NET Core Hosting Bundle installed.
In this tutorial, we are going to explain how web.config
plays role in hosting ASP.NET Core Web Application on IIS.
Web.config with .NET Core
We already know that web.config
is not used as the configuration file for ASP .NET Core web applications. Those applications now use appsettings.json
, a JSON configuration file.
IIS uses the ASP.NET Core Module to handle the ASP .NET Core application’s requests. The module is an HTTP handler. The handler has some settings which are read by IIS. The handler has settings like processPath
, arguments
, hostingModel
. These settings decide which .NET core application should be invoked and what is the hosting model for the application.
How to Implement it?
If a web.config file is not present at root, then it is created with the correct processPath
and arguments
during the publish operation.
If a web.config is already present at app’s root, then it is transformed to use correct values for processPath
and arguments
settings and it is copied to published output directory.
For these files, InheritInChildApplications property is set to false to avoid applying these settings to the apps that reside in subdirectory of this app.
Web.Config vs Deployment Modes
The file as indicated below is generated (or required) by framework-dependent deployment.
<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\DemoApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" /> </system.webServer> </location> </configuration>
While, Self-Contained deployment
generates the web.config file as shown below:
<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath=".\DemoApp.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" /> </system.webServer> </location> </configuration>
Changes to Files Served
Different types of files are included in the Content Root of the site when an app is published. The published folder contains the files listed below.
- {Assembly-Name}.runtimeconfig.json
- {Assembly-Name}.deps.json
- {Assembly-Name}.xml
In above list, {Assembly-Name} is the placeholder for name of assembly.
If a valid web.config file is present at the root, these files would not be served by IIS. Otherwise, these files would be served publicly.
Set Environment Variables
If you want to keep your own web.config
file for your app, you can also set the environment variables via web.config
.
The aspNetCore
element can contain environmentVariables
xml element, where the environment variables can be specified. These values would be applicable to the process specified using processPath
setting.
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\DemoApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> <environmentVariable name="CONFIG_DIR" value="f:\application_config" /> </environmentVariables> </aspNetCore> </system.webServer> </location> </configuration>
Conclusion
We hope that above article is useful for you and you have known that web.config play role vital on your Asp.net deployment.
If you are looking for fast and secure ASP.NET hosting, you can visit our site at https://www.asphostportal.com. Our fully featured hosting already includes
- Easy setup
- 24/7/365 technical support
- Top level speed and security
- Super cache server performance to increase your website speed
- Top 9 data centers across the world that you can choose.
Javier is Content Specialist and also .NET developer. He writes helpful guides and articles, assist with other marketing and .NET community work