How to deploy your .NET Core application to IIS

13 feb 2024 3 min di lettura
How to deploy your .NET Core application to IIS
Indice dei contenuti

Introduction

Deploying a.NET Core application to Internet Information Services (IIS) may seem complex at first, but with the right steps it becomes a simple process. This guide will walk you through deploying your.NET Core application to IIS, from prerequisites to troubleshooting common issues.

Prerequisites

Before you get started, make sure you have the following:

  • .NET Core Runtime and Hosting Package - Installed on the server running IIS. This bundle allows IIS to host.NET Core applications.
  • IIS configured on Windows Server - Make sure IIS is installed and running. You can install it via Windows Server Manager or PowerShell.
  • Administrator access: You will need administrator access to the server to install the hosting package and configure IIS.

Step 1: Install the.NET Core hosting package

Visit the official.NET download page.

Select the required version and download the .NET Core hosting package

Run the installer on your server. The.NET Core Runtime, .NET Core Library, and the ASP.NET Core module will be installed. The ASP.NET Core module is a component required to host.NET Core applications in IIS.

Restart IIS to load the newly installed modules. You can do this by running iisreset in the command prompt.

Step 2: Publish your.NET Core application

Using Visual Studio

Open your solution in Visual Studio.

Right-click the project in Solution Explorer and select "Publish".

Choose "Folder" as the publish destination and specify a folder to publish the application files to.

Click "Publish", Visual Studio will compile your application and copy the necessary files to the specified folder.

Using Command Prompt

To publish your.NET Core application, navigate to the project directory on the command line. Then, run the following command:

dotnet publish -c Release

This command compiles the application in release mode and sends the output to the "bin/Release/netcoreapp{version}/publish" directory.

Step 3: Create an IIS application pool

Using IIS Manager

Open IIS Manager.

In the "Connections" pane, right-click "Application Pools" and select "Add Application Pool".

Name the application pool and set the.NET CLR version to "No Managed Code" since.NET Core does not rely on the CLR runtime. Managed pipeline mode must be set to "Integrated".

Click "OK" to create the application pool.

Using Command Prompt

Open Command Prompt as administrator.

Run the following commands:

cd %windir%\system32\inetsrv
 appcmd add apppool /name:"DotNetCorePool" /managedRuntimeVersion:"" /managedPipelineMode:"Integrated"

Replace "DotNetCorePool" with the name you want to give your new application pool.

Step 4: Deploy the application to IIS

Using IIS Manager

In IIS Manager, right-click "Sites" in the "Connections" pane and choose "Add Website".

Enter a name for your website and select the application pool you created earlier.

For the physical path, browse and select the folder where you published your.NET Core application.

Specify binding information (for example, IP address, port, and host name).

Click "OK" to add the website.

Using Command Prompt

You can also create a website in IIS using Command Prompt. To create a website, type:

cd %windir%\system32\inetsrv
 appcmd add site /name:MyWebsite /physicalPath:C:\inetpub\wwwroot\MyApp /bindings:http/*:80:example.com /apppool:DotNetCorePool

Replace "MyWebsite" with the name of your website, C:\inetpub\wwwroot\MyApp with the path to your application, example.com with your domain name, and DotNetCorePool with the application pool you created in the previous step.

Step 5: Configure the ASP.NET Core module

Make sure the web.config file is configured correctly for the ASP.NET Core module to forward HTTP requests to your application. This file must be included in the published output and contain settings such as the following:

<configuration>
 <system.webServer>
 <handlers>
 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
 </handlers>
 <aspNetCore processPath="dotnet" arguments=".\YourApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
 </system.webServer>
 </configuration>

Replace YourApplication.dll with the name of your application dll.

Step 6: Test your application

Open a web browser and go to the URL you configured for your site. If everything is configured correctly, your.NET Core application should be running on IIS.

Solving common problems

HTTP Error 502.5 – Process Error: Often indicates a problem with the application startup process. Check the event logs and stdout logs (if enabled) for specific errors.

Internal Server Error 500: This can occur for various reasons, including problems with the web.config file or application dependencies. Again, logs are invaluable in diagnosing these problems.

404 File or directory not found - This can occur if the physical path of the application is incorrect or if the application pool is configured incorrectly.

Conclusion

Deploying a.NET Core application to IIS involves several steps, from installing the.NET Core hosting bundle to configuring IIS and publishing the application. By following this guide you can ensure a smooth deployment process and have your.NET Core application running on IIS in no time. Remember, troubleshooting is an integral part of deployment, so don't be put off by initial hiccups; use the logs and error messages as guides to troubleshoot any problems.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.