.NET IIS Deployment
Introduction
Deploying your .NET application to Internet Information Services (IIS) is a common approach when hosting web applications on Windows servers. IIS (Internet Information Services) is Microsoft's web server that comes built into Windows operating systems. It provides a robust, secure, and scalable environment for hosting .NET applications.
In this guide, we'll walk through the process of deploying a .NET application to IIS, from preparing your application to configuring the server and maintaining your deployment. Whether you're deploying an ASP.NET Core, ASP.NET MVC, or other .NET web application, this guide will help you navigate the deployment process.
Prerequisites
Before we start with deployment, make sure you have:
- A .NET application ready for deployment
- Windows server with IIS installed (or Windows 10/11 with IIS enabled)
- Administrative access to the server
- .NET Core/.NET hosting bundle installed (for .NET Core/.NET applications)
Setting Up IIS
Installing IIS
If you don't have IIS installed already, you'll need to enable it:
-
On Windows Server:
- Open Server Manager
- Click "Add roles and features"
- Select "Web Server (IIS)" role
- Follow the wizard to complete installation
-
On Windows 10/11:
- Go to Control Panel
- Navigate to "Programs and Features" → "Turn Windows features on or off"
- Check "Internet Information Services" and expand to select desired features
- Click OK to install
Installing .NET Hosting Bundle
For .NET Core or .NET 5+ applications, you need to install the appropriate hosting bundle:
- Download the hosting bundle for your .NET version from Microsoft's download page
- Run the installer with administrative privileges
- Follow the installation steps
- Restart the server or IIS after installation
Preparing Your Application for Deployment
Publishing Your Application
Before deploying to IIS, you need to publish your application:
# Using the .NET CLI
dotnet publish -c Release -o ./publish
# Using Visual Studio: Right-click project -> Publish -> Choose Folder target
The publish process creates a deployment-ready version of your application with all dependencies.
Understanding the Published Output
The published output will contain:
- Your application's DLLs
- Web.config or appsettings.json configuration files
- Static content (CSS, JS, images)
- Runtime dependencies
Creating an IIS Website
Now that your application is published, let's create a website in IIS:
- Open IIS Manager (type
inetmgr
in the Run dialog) - In the Connections panel, right-click on "Sites" and select "Add Website"
- Fill in the website details:
- Site name: A meaningful name for your application
- Physical path: The directory where your published files are located
- Binding: Configure the hostname, IP address, and port
- Click OK to create the website
Here's a visual representation of what you'll configure:
Site name: MyDotNetApp
Physical path: C:\inetpub\wwwroot\MyDotNetApp
Binding type: http
IP address: All Unassigned
Port: 80
Host name: mydotnetapp.local
Configuring Application Pools
Application pools isolate web applications from each other, providing better security and reliability:
- In IIS Manager, click on "Application Pools"
- Right-click and select "Add Application Pool"
- Configure the pool settings:
- Name: Match your website name for clarity
- .NET CLR Version:
- For .NET Framework: Choose appropriate version
- For .NET Core/.NET: Select "No Managed Code" as .NET Core has its own runtime
- Managed pipeline mode: Typically "Integrated"
- Click OK to create the application pool
- Go back to your website, right-click and select "Basic Settings"
- Select the application pool you created and click OK
Web.config Configuration
The web.config
file plays a crucial role in IIS deployment. For .NET Core/.NET 5+ applications, it's generated during publishing, but you may need to customize it.
For .NET Core/.NET 5+ Applications
Here's an example web.config
for an ASP.NET Core application:
<?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=".\MyApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
For .NET Framework Applications
.NET Framework applications typically have more detailed web.config files:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.8" />
<httpRuntime targetFramework="4.8" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
Setting Up Permissions
Proper permissions are crucial for your application to run correctly:
- Locate your website's folder
- Right-click on it and select "Properties"
- Go to the "Security" tab
- Click "Edit" and then "Add"
- Add the appropriate IIS user:
- For application pool identity:
IIS AppPool\[YourAppPoolName]
- For IIS_IUSRS group (all IIS applications)
- For application pool identity:
- Grant at least "Read" and "Execute" permissions
- For folders requiring write access (like for file uploads), add "Modify" permissions
Testing Your Deployment
After setting up your website, it's time to test it:
- Open a web browser on the server
- Navigate to
http://localhost
- If you've set up a specific hostname, make sure it's in your hosts file (
C:\Windows\System32\drivers\etc\hosts
) - Verify that your application loads correctly
Common Deployment Scenarios
Deploying to a Sub-application
You might want to deploy your application as a sub-application under an existing website:
- In IIS Manager, right-click on an existing website
- Select "Add Application"
- Set an alias (e.g., "myapp")
- Select the physical path to your published files
- Choose the application pool
- Click OK
Now your application will be available at http://yourmaindomain/myapp
.
Setting Up HTTPS with SSL
For secure communication, you should enable HTTPS:
- Obtain an SSL certificate (either from a certificate authority or create a self-signed certificate)
- In IIS Manager, select your website
- Click "Bindings" in the Actions panel
- Click "Add" to add a new binding
- Select "https" from the Type dropdown
- Select your SSL certificate
- Click OK
Type: https
IP address: All Unassigned
Port: 443
Host name: mydotnetapp.local
SSL certificate: [Select your certificate]
Configuring URL Rewrite Rules
URL rewriting can be useful for clean URLs or redirecting HTTP to HTTPS:
- Install the URL Rewrite module for IIS if not already installed
- In IIS Manager, select your website
- Double-click the URL Rewrite icon
- Click "Add Rule(s)" in the Actions panel
Here's an example web.config
snippet for HTTP to HTTPS redirection:
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Troubleshooting Common Issues
Application Pool Crashes
If your application pool keeps crashing:
- Check the Windows Event Viewer for error details
- Verify your application's dependencies
- Ensure correct .NET version is installed
- Check permissions on application folders
500 Internal Server Errors
When you see generic 500 errors:
- Enable detailed error messages in IIS
- Select your site in IIS Manager
- Double-click "Error Pages"
- Click "Edit Feature Settings"
- Select "Detailed errors"
- Check application logs
- Review the Windows Event Viewer
502.5 Process Failure
This typically occurs in .NET Core applications when the process can't start:
- Make sure the .NET Hosting Bundle is installed
- Verify application pool settings
- Check if your app runs from command line with
dotnet YourApp.dll
- Look for logs in the
logs
directory of your application
Best Practices for IIS Deployment
-
Use deployment automation: Tools like GitHub Actions, Azure DevOps, or Jenkins can automate your deployment process
-
Implement proper logging: Configure application logging to help troubleshoot issues:
// Program.cs in ASP.NET Core
builder.Host.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddEventLog();
logging.AddFile("logs/app-{Date}.log");
});
-
Set up health checks: Implement health check endpoints to monitor your application's status
-
Configure appropriate recycling: Set up application pool recycling based on your application's needs
-
Set up monitoring: Use tools like Application Insights or other monitoring solutions
Deployment Scripts and Automation
To automate your deployment process, you can create PowerShell scripts:
# Example PowerShell deployment script
param (
[string]$WebsiteName = "MyDotNetApp",
[string]$SourcePath = "C:\Builds\MyDotNetApp\publish\",
[string]$DestinationPath = "C:\inetpub\wwwroot\MyDotNetApp\"
)
# Stop the website
Write-Host "Stopping website $WebsiteName..."
Stop-WebSite -Name $WebsiteName
# Copy files
Write-Host "Copying files from $SourcePath to $DestinationPath..."
if (Test-Path $DestinationPath) {
Remove-Item -Path "$DestinationPath\*" -Recurse -Force
}
else {
New-Item -ItemType Directory -Path $DestinationPath -Force
}
Copy-Item -Path "$SourcePath\*" -Destination $DestinationPath -Recurse -Force
# Start the website
Write-Host "Starting website $WebsiteName..."
Start-WebSite -Name $WebsiteName
Write-Host "Deployment complete!"
Summary
Deploying .NET applications to IIS provides a robust hosting environment for your web applications. In this guide, we've covered:
- Setting up IIS and installing the necessary components
- Publishing your .NET application
- Creating websites and application pools in IIS
- Configuring proper permissions and settings
- Setting up SSL and URL rewriting
- Troubleshooting common issues
- Best practices and automation
By following these steps, you should be able to successfully deploy your .NET application to IIS and ensure it runs reliably in a production environment.
Additional Resources
- Official Microsoft Documentation on IIS Hosting
- .NET Core Hosting Bundle
- IIS URL Rewrite Module
- Application Request Routing for IIS
Exercises
- Deploy a simple ASP.NET Core application to IIS and configure it to use HTTPS.
- Create a PowerShell script that automates the deployment process for your application.
- Set up URL rewrite rules to redirect all traffic from HTTP to HTTPS.
- Configure your application to use a custom application pool with specific recycling settings.
- Implement logging in your application and configure it to write logs to a specific folder accessible by IIS.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)