Lessons in this course (12)

12. Deploying an ASP.NET Core site to IIS and to Azure App Service

Intermediate★ Must knowLesson 12 of 12

IIS on a Windows server (VM or on-premises):

powershell
# 1. Server: enable IIS and install the .NET Hosting Bundle (runtime + ASP.NET Core Module)
Install-WindowsFeature -Name Web-Server -IncludeManagementTools
# download & run dotnet-hosting-<version>-win.exe, then: net stop was /y; net start w3svc

# 2. Build: publish the app
dotnet publish .\Shop.Web\Shop.Web.csproj -c Release -o C:\publish\shop

# 3. IIS: app pool with "No Managed Code", site pointing to C:\publish\shop, bindings 80/443 + certificate
Import-Module WebAdministration
New-WebAppPool -Name "ShopPool"
Set-ItemProperty IIS:\AppPools\ShopPool -Name managedRuntimeVersion -Value ""
New-Website -Name "Shop" -PhysicalPath "C:\publish\shop" -ApplicationPool "ShopPool" -Port 80 -HostHeader "www.shop.com"

Then: give the app pool identity read access to the folder (and write access to log folders), set environment (ASPNETCORE_ENVIRONMENT) and connection strings (environment variables or secured config), open firewall ports 80/443, test locally on the server, then from outside.

Azure App Service:

bash
az group create -n rg-shop -l centralindia
az appservice plan create -n plan-shop -g rg-shop --sku B1 --is-linux
az webapp create -n shop-web-divakar -g rg-shop -p plan-shop --runtime "DOTNETCORE:10.0"
dotnet publish -c Release -o ./publish && cd publish && zip -r ../app.zip . && cd ..
az webapp deploy -n shop-web-divakar -g rg-shop --src-path app.zip --type zip
az webapp config appsettings set -n shop-web-divakar -g rg-shop --settings ASPNETCORE_ENVIRONMENT=Production

Then: custom domain + managed certificate, connection string to Azure SQL (prefer Managed Identity), Application Insights, deployment slots (staging → swap) and a CI/CD pipeline instead of manual deploys.

Troubleshooting a "site not working":

SymptomLikely cause
DNS_PROBE_FINISHED_NXDOMAINDNS record missing/wrong or not propagated
Connection timed outFirewall/NSG blocks the port, server down, wrong IP
Certificate warningWrong hostname, expired, missing intermediate certificate
502.5 / 500.30 on IISASP.NET Core app failed to start — Hosting Bundle missing, wrong runtime, startup exception (enable stdout log, check Event Viewer)
503 Service UnavailableApp pool stopped/crashing, overloaded
404 for deep links on an SPAServer not rewriting unknown paths to index.html
Works locally, slow liveDatabase latency (different region), no caching, small plan