This article will teach you how you can deploy or host your ASP.NET Core 2.0 web application on IIS. The hosting of ASP.NET Core 2.0 is a little different from hosting in ASP.NET. So, let’s understand it step by step.
The program class in asp.net core 2.0 contains a method that is called “CreateDefaultBuilder”. It is responsible for setting up everything for your application related to hosting, managing servers, IIS integration, creating directories, etc.
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Just go through with CreateDefaultBuilder method's actual definition using F12 in Visual Studio, you can read the comment that specifies its tasks.
The deployment is not the same as Asp.Net, there are a few more components required to host your Asp.net core 2.0 application on IIS. Before deploying, you have to install a bundle that is required to host the Asp.Net Core 2.0 application on IIS and that is .Net Core Windows Server Hosting. This bundle will install .Net Core Runtime which provides all the required libraries on runtime, .Net Core Library, and Asp.Net Core Module.
If you are still using Asp.Net Core 1.x then you can find out the “.Net Core Windows Server Hosting” bundle using the following link and install it.
https://go.microsoft.com/fwlink/?LinkId=817246
Here we are using the Asp.Net Core 2.0 application to deploy it on IIS, so we are going to download the .Net Core Windows Server Hosting bundle from the following link and install it with our system.
https://aka.ms/dotnetcore-2-windowshosting
When it is in progress, you can see it is installing three main components: .Net Core Runtime, .Net Core Library, and Asp.Net Core Module. As you can see in the following image, Microsoft .Net Core Runtime is being installed.
Warning
Please make sure you have restarted your system before moving to the next step after the installation of “Microsoft .Net Core Windows Server Hosting” bundles.
For this demonstration, we are using the same Asp.Net Core 2.0 application that we created in the previous article. To learn how to create the First application in Asp.Net Core 2, please refer to the following article.
First Application In ASP.NET Core MVC 2.0
Let’s move to Visual Studio 2017 version 15.3 which provides .Net Core 2 features. Open the Asp.Net Core 2.0 application which we have created in the last article. Open the solution explorer and right click on the project and choose the Publish option for publishing this web application.
It will open a new window as follows, which provides us with three different options to publish our web application and these options are “Azure”, “IIS, FTP” and “Folder”. So, here are choosing “Folder” to publish our web content”. You have to provide the destination path where you would like to publish the web application and then click “Publish”.
It will start publishing content on the selected folder as the following image shows. Once everything is fine, it will show the Publish Successes message.
Now it’s time to create a new website inside the IIS Manager, to open IIS Manager, just type “inetmgr” in “Search Program and files” in the start menu. It will open IIS Manager for you as the following image shows where you can manage your application.
Just right-click on “Sites” and choose “Add Web Site..”.
In the next window, you have to define your “Site Name”, the physical path where you published your application earlier, and the hostname to find on the web browser as in below image. Don’t change Port or anything else and just click OK.
Now we are almost done but need to change the “Application Pools” for this site. Click on application pools from the left panel choose your pools as “asp.netcore2.com” and double-click on that. Now you can edit your “Application Pool” for this website. From this window, you have to change “.Net Framework version” or “.Net CLR version” with “No Managed Code” and OK.
The last thing, you need to do, is just make one entry about your application inside the host file, which is located in the following location. You have to add one more entry as we have done for asp.netcore2.com which directly points to a localhost IP address.
C:\Windows\System32\drivers\etc
The time has come to run the application on the browser, so open any browser and just type asp.netcore2.com and press enter. Wow… you will get the following screen, which means the application has successfully hosted on IIS.
Conclusion
So, today we have learned how to host your Asp.Net Core 2.0 application on IIS.
I hope this post will help you. Please put your feedback using comments which helps me to improve myself for the next post. If you have any doubts please ask in the comment section and if you like this post, please share it with your friends.
Thanks!