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:
- Application Pools: These provide a way to isolate applications from each other. Each application pool has its own worker process, which means if one application crashes, it won't affect others.
- Web Sites: A website in IIS is a collection of files and directories that is accessible via a specific hostname, IP address, and port.
- Virtual Directories and Applications: You can map URLs to specific physical directories on your server. An application is a specific collection of resources within a website that is treated as a single unit by IIS.
Prerequisites
Before hosting your .NET application on IIS, ensure that:
- IIS is installed and configured on your Windows server.
- The ASP.NET Core Module (ANCM) is installed. This module is essential for IIS to host ASP.NET Core applications.
- 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:
- Open IIS Manager.
- Right-click on "Sites" in the Connections pane and select "Add Website...".
- Provide a Site name (e.g., "MyDotNetApp").
- Set the Physical path to the publish output folder created in step 1.
- Set the Port (e.g., 80 for HTTP, or another port if 80 is in use).
- Click "OK".
3. Configure the Application Pool
Ensure your application pool is configured correctly:
- In IIS Manager, select the application pool associated with your website.
- On the right-hand pane, under "Actions", click "Basic Settings...".
- Ensure the ".NET CLR Version" is set to "No Managed Code" for ASP.NET Core applications. This is because ASP.NET Core runs in its own process managed by the ANCM.
- Set the "Managed pipeline mode" to "Integrated".
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>
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
- Check IIS Logs: IIS logs are located in
%SystemDrive%\inetpub\logs\LogFiles\W3SVC[site_id]
. - Enable ANCM Logging: Configure logging for the ASP.NET Core Module in
web.config
usingstdoutLogEnabled
andstdoutLogFile
. - Application Pool Identity: Ensure the application pool identity has necessary permissions to access application files and directories.