In this post, I will explain how to host\deploy your .Net Core 2.0 application to IIS.
- Visual studio 2017 community edition, download here
- .Net Core 2.0 SDK from here (I have written a post to install SDK here)
- Make sure you have enabled the Web Server (IIS) role and established the role services
Install .NET Core Windows Server Hosting Bundle
Since .Net Core is a new framework, at this moment IIS is not aware of how to run .Net Core applications. So we need to inform IIS to set up an environment for the .Net Core applications.
For this, we need to install .Net Core Windows Server Hosting bundle which will install the .NET Core runtime, libraries, and the ASP.NET Core module for IIS.
Please note that this is a very important step and you must install the bundle before you publish the application on IIS.
You can download it from here(download will start once you click the link):
Wait till the installation is over:
Once the installation is completed, either restart your system or run below commands in sequence in command prompt:
- net stop was /y
- net start w3svc
The first command will stop the World Wide Web publishing service and the second command will start the service again.
Create the Web application using .Net Core 2.0 template in VS 2017
Once you have all these installed, open your Visual Studio 2017 -> Create New Project -> Select Core Web application:
Click on Web Application in next window and then click on Ok:
Visual Studio will create a well-structured application for you.
Right click on the solution and click on Publish:
In next window, click on Folders and give the folder path. I created Publish folder under wwwroot folder:
It will publish the application on the above-mentioned path:
Create Website on IIS
Open your IIS Manager and add new website:
Now go to Application Pools panel, open the Edit Application Pool window by right-clicking on the website’s app pool and selecting Basic Settings and set .NET CLR version to No Managed Code:
Check if .Net Core Windows Server Hosting bundle is installed properly
Next step is to check whether the hosting bundle which we installed earlier is installed properly or not.
For this click on Modules(in IIS Manager) of your newly created website and search for AspNetCoreModule. If it is available, it indicates that IIS is now aware of how to run a .Net Core application.
You might be wondering what is Asp .Net Core Module?
- ASP.NET Core Module lets you run ASP.NET Core applications on IIS for what it’s good for i.e. security, manageability, and lots more
- On the other hand, ASP.NET Core Module lets you run ASP.NET Core applications using Kestrel for what it’s good at for example being really fast
- Thus it is getting the benefits of both technologies at once
- Asp .Net Core Module handles all incoming traffic to IIS and acts as the reverse proxy that knows how to hand the traffic off to your ASP.NET Core application
You may have noticed that when we published the code to a particular folder, a web.config was created in which you can see aspNetCore under Handler section as below. This is only used when deploying your application to IIS and It registers the AspNetCoreModule as an HTTP handler.
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <strong><add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /></strong> </handlers> <aspNetCore processPath="dotnet" arguments=".\NeelCorePublishIIIS.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> </system.webServer> </configuration>
That is it.
Just load your IIS site and it should load just fine:
Now the application is hosted on IIS.
Hope it helps.