Wednesday, January 1, 2020

How to host ASP.NET Core 2.0 Application on IIS ?

This article will teach you how you can deploy or host you 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.
Program class in core 2.0 contains a method that is called “CreateDefaultBuilder”. It is responsible for setting up everything for your application related to hosting, manage server, IIS integration, create directory etc.
  1. public class Program {  
  2.     public static void Main(string[] args) {  
  3.         BuildWebHost(args).Run();  
  4.     }  
  5.     public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup < Startup > ().Build();  
  6. }  

Just go through with CreateDefaultBuilder method's actual definition using F12 in Visual Studio, you can read the comment which specifies its tasks.
The deployment is not the same as Asp.Net, there are a few more components required to host your core 2.0 application on IIS. Before deploying, you have to install a bundle which is required to host 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.
Here we are using Asp.Net Core 2.0 application to deploy it on IIS, so we are going to download .Net Core Windows Server Hosting bundle from following link and install it with our system.
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.

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 which we have created in previous article. To learn how to create First application in Asp.Net Core 2, please refer to following article.
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 last article. Open the solution explorer and right click to project and choose Publish option for publishing this web application.
It will open a new windows as following, which provide us 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 web application and then click to “Publish”.
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 start menu. It will open IIS Manager for you as following image shown where you can manage you application.
Just right click to “Sites” and choose “Add Web Site..”.
Now we are almost done but need to change the “Application Pools” for this site. Click to application pools from the left panel and choose you pools as “” and double click on that. Now you can edit you “Application Pool” for this website. From this window, you have to changes “.Net Framework  version” or “.Net CLR version” with “No Managed Code” and OK. 
Last thing, what you need to do, just make one entry about your application inside the host file, which is located on the following location. You have to add one more entry as we have done for which directly points to localhost IP address.
The time has come to run the application on the browser, so open any browser and just type and press enter. Wow… you will get the following screen, which means the application has successfully hosted on IIS.
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 comment 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

1 comment: