I have published some posts about basics of Kestrel server, which comes with .NET. The Kestrel server can be used with other web server like IIS or NGINX.
In this article, let’s have a look at how IIS can be used to host ASP .NET Core web applications.
Create Web App
For this demo, we will use the default project template for ASP .NET Core MVC web application. The project can be created using Visual Studio
or dotnet CLI
.
For hosting this application in IIS, there are two pre-requisites.
- First, have right .NET Core SDK installed on the development machine. If you can create the demo project, it means this is already installed on your machine.
- Second, enable IIS via turn windows features on and off.
Install .NET Core Hosting Bundle
.NET Core hosting bundle installs .NET Core Runtime, .NET Core Library and ASP .NET Core Module. The current .NET Core hosting bundle can be downloaded from this direct download link. Install this downloaded installer and then after this, restart the IIS server.
ASP .NET Core module
is a native IIS module, which enables running .NET Core based web applications on IIS. Using this module, the .NET Core based web app can run either in in-process
mode or in out-of-process
mode. When .NET Core web app is running in out-of-process
, IIS server acts as a reverse proxy.
Generally, in-process mode is faster as the .NET Core application is running as a part of same process and hence there no additional overheads of forwarding request to another process.
IIS Configurations
Before we start publishing the application to IIS, we will need to create the website in IIS, which will host the application.
Open the IIS Manager UI and then right click on Sites and select Add Website… option to create a new web site.
Next, the Add Website dialog opens. It asks some basic details about site:
- Site Name, the name of new site being created.
- Application pool, the application pool specifies identity under which site runs, the framework version and some other details. Please refer IIS documentation for more details.
- Physical Path, the path where the code – published folder of your web app – should be available.
- Binding, this is the URL binding for IIS. Provide port, IP Address and protocol which this app should listen to. The binding should be unique to the site being created. For ex. if HTTP and port 80 binding is already assigned to some other site, new site should be assigned binding in such a way that the combination is unique.
- SSL Certificate, if HTTPS protocol is selected, then SSL certificate should be provided. For this demo, select the IIS Express Development Certificate option as shown in below snapshot.
Then click on OK to create the website.
At this moment, if you try to browse localhost, it would return 500 Internal Server Error, IIS page.
Publish
Now, let’s come back to Visual Studio, where we created ASP .NET Core MVC web app, we want to publish. Right click on the web project and select Publish option from the context menu.
It would open a Publish dialog. The dialog shows different options, we can select any one option depending on the need. The application can be published to Azure web app, or Docker container registry or some File Share (using Folder option), or some FTP server.
There is also option to deploy on IIS directly, but for this demo, let’s select Folder option.
Selecting Folder option and then clicking on Next would open next screen, which asks for the folder location, where the site should be published. This path can be either from the same machine or it can be some shared path on the network.
For this demo, let’s provide the same path which is mapped with IIS web site, which was created in previous step. Then click on Finish.
This would finish the wizard and would show the publish profile settings – as shown in the below image. If you click on Show all link, it would open another dialog, where additional settings can be configured.
On the new Publish dialog, you can set:
- Configuration, debug or release
- Target Framework, the framework that should be used to build the app
- Deployment Mode, There are two possible values.
Framework-Dependent
orSelf-contained
.Framework-Dependent
mode means only application specific libraries and executables are copied to the publish location. IfSelf-Contained
option is selected, it would copy the .NET Core runtime and other libraries required for the app. - Target Runtime, the portal mode is default value here. But it can be changed to other options, e.g. x86, x64 (Windows or Linux or MacOS)
- File Publish Options, if this check box is checked, publish operation would delete existing files before copying new contents.
Then click on Save to save these options and close the popup.
Then click on Publish button to build and publish the application to IIS.
Run and Verify
Now, let’s try to access the application using URL – https://localhost:9443
. It should open the default index page provided by the project template.
So, we have successfully published the .NET core web application to IIS. But we have not discussed if this is an in-process or reverse-proxy configuration. Can you guess ? Let me know your thoughts.