Tuesday 2 February 2021

IIS - How To Host A .NET Core Application In 10 Steps

 If you started developing with .NET Core and you can’t leave your Windows hosting server, then you need to learn how to set up the IIS for it. Follow these steps and you will be good to go.

 
IIS - How To Host A .NET Core Application In 10 Steps

Developing with .NET Core is not too different from developing with .NET Framework, but setting up the IIS to host a .NET Core application… ow… it needs some tricks.

I’m trying to be as clear as possible. For this article, I am using the source code that I wrote in my .NET Core for .NET Developers article. Click here to download it.

Installations on Windows Machine

Step 1

Before setting up the IIS, it’s required to install .NET Core Runtime. For this article, I’ve used version 2.2. Without that, .NET Core won’t work. Remember, maybe it’s possible to reboot Windows.

Step 2

After that, you need to install the .NET Core Windows Server Hosting bundle, and you can download it here. Without that, IIS won’t work with .NET Core. You will need to reboot Windows.
 
IIS - How To Host A .NET Core Application In 10 Steps
 

Changes in the .NET Core web project

Step 3

Open the FSL.NetCoreBasics.Mvc.csproj project file using notepad, locate the tag AspNetCoreHostingModel and change it to AspNetCoreHostingModelV2.
  1. <AspNetCoreHostingModelV2>InProcess</AspNetCoreHostingModelV2>  
After that, you will need to restore NuGet package and compile the application.
 

Changes in the C# code

Step 4

In Visual Studio, open the project and, then open the Program.cs file, locate a method called Main and make the changes like the code below.
  1. public static void Main(string[] args)  
  2. {  
  3. var host = new WebHostBuilder()  
  4.         .UseKestrel()  
  5.              .UseContentRoot(Directory.GetCurrentDirectory())  
  6.              .UseIISIntegration()  
  7.              .UseStartup<Startup>()  
  8.              .Build();  
  9.     host.Run();  
  10.  }  
You can delete the CreateWebHostBuilder method because it isn’t necessary anymore.

Step 05

Open the Startup.cs file, locate a method called ConfigureServices and add the following commands:
  1. services.Configure<IISOptions>(options =>  
  2. {  
  3. options.AutomaticAuthentication = false;  
  4. });  

Adding a web.config file

Step 06

Yes my friend, it’s required to add a web.config file to our project to host a .NET Core application on IIS. Create that file and copy/paste the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.     <system.webServer>  
  4.         <handlers>  
  5.             <add name="aspNetCore"  
  6.                  path="*"  
  7.                  verb="*"  
  8.                  modules="AspNetCoreModule"  
  9.                  resourceType="Unspecified"/>  
  10.         </handlers>  
  11.         <aspNetCore processPath="%LAUNCHER_PATH%"  
  12.                     arguments="%LAUNCHER_ARGS%"  
  13.                     stdoutLogEnabled="false"  
  14.                     stdoutLogFile=".\logs\stdout"  
  15.                     forwardWindowsAuthToken="false"/>  
  16.     </system.webServer>  
  17. </configuration>  

Special thanks to Macoratti for that piece of code.

Setting up the IIS

The next step is to set up an Application Pool and a virtual directory (or website) in IIS to run a .NET Core application.

Step 7

Open the IIS, add a new Application Pool, then choose No Managed Code for .NET Framework version field and finally, select Integrated for Managed pipeline mode field.
 
IIS - How To Host A .NET Core Application In 10 Steps
 
Step 8
 
Add a new folder to host your application on IIS and create a virtual directory for it. Don’t forget to choose the Application Pool that you’ve created in Step 7.
 
IIS - How To Host A .NET Core Application In 10 Steps
 

Publishing on IIS

Step 9

Open the project in Visual Studio, click with the mouse’s right button on the project and choose the Publish option. Select the Folder tab on the left, fill or select your application folder and click Publish.
 
IIS - How To Host A .NET Core Application In 10 Steps
 
If you open Windows Explorer and navigate to your application folder, you will find all DLLs, the web.config file, and a wwwroot folder.
 
That’s your application published on IIS.
 
IIS - How To Host A .NET Core Application In 10 Steps
 

Testing the application

Step 10

Open your favorite browser and type http://localhost/FSL.NetCoreBasics.Mvc. Your .NET Core application will be started, running on IIS.
 
IIS - How To Host A .NET Core Application In 10 Steps
That’s it! Did you enjoy it? Have you found an error?

Please comment and share!

No comments:

Post a Comment