Hosting .NET Applications on Internet Information Services (IIS)

Internet Information Services (IIS) is a powerful and extensible web server from Microsoft. It is commonly used for hosting ASP.NET applications, but it can also serve static content and host other web technologies.

Overview

IIS integrates with the .NET runtime to host applications. Key concepts include:

Prerequisites

Before hosting your .NET application on IIS, ensure that:

  1. IIS is installed and configured on your Windows server.
  2. The ASP.NET Core Module (ANCM) is installed. This module is essential for IIS to host ASP.NET Core applications.
  3. Your .NET application is published for the correct target framework and runtime.

Steps to Host an ASP.NET Core Application

1. Publish Your Application

Publish your ASP.NET Core application to a folder. For example:

dotnet publish -c Release -o ./publish

2. Configure IIS

Follow these steps to configure IIS:

  1. Open IIS Manager.
  2. Right-click on "Sites" in the Connections pane and select "Add Website...".
  3. Provide a Site name (e.g., "MyDotNetApp").
  4. Set the Physical path to the publish output folder created in step 1.
  5. Set the Port (e.g., 80 for HTTP, or another port if 80 is in use).
  6. Click "OK".

3. Configure the Application Pool

Ensure your application pool is configured correctly:

Common Configuration Options

Web.config File

The web.config file in your publish output directory controls how IIS handles your application. A typical web.config for an ASP.NET Core application looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\MyDotNetApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>
Note: For ASP.NET Core applications, it's common to set processPath to dotnet and arguments to the name of your application's DLL. The hostingModel can be set to inprocess (recommended for performance) or outofprocess.

IIS Rewrite Module

You might need the IIS URL Rewrite module to handle routing correctly, especially for Single Page Applications (SPAs).

Troubleshooting

Tip: For seamless deployment and management, consider using tools like Web Deploy or Azure DevOps pipelines.