MSDN Blog

Exploring the latest in .NET and Azure
Author Avatar By Jane Doe | Published: October 26, 2023
Category: .NET, Azure, App Service

Deploying Your ASP.NET Core Application to Azure App Service

Azure App Service is a fully managed platform that enables you to build, deploy, and scale web apps, mobile back ends, and API apps. In this post, we'll walk through the process of deploying a typical ASP.NET Core application to Azure App Service, covering essential steps and best practices.

Prerequisites

Step 1: Prepare Your ASP.NET Core Application

Ensure your application is configured correctly for deployment. For most ASP.NET Core apps, this involves setting the correct environment variables and ensuring production settings are enabled.

For example, in your Program.cs (for .NET 6+):

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Step 2: Create an Azure App Service Resource

You can create an App Service in several ways:

1

Azure Portal

Navigate to the Azure portal, search for "App Services," and click "Create App Service." Fill in the required details, including subscription, resource group, name, publish method (Code), runtime stack (.NET), and region.

2

Azure CLI

Use the following command to create a new App Service Plan and then the App Service itself:

az group create --name MyResourceGroup --location eastus
az appservice plan create --name MyPlan --resource-group MyResourceGroup --sku S1
az webapp create --resource-group MyResourceGroup --plan MyPlan --name MyAspNetCoreApp

Step 3: Deploy Your Application

Once your App Service is created, you can deploy your ASP.NET Core application:

Option A: Using Visual Studio

If you have Visual Studio installed, right-click on your project in Solution Explorer, select "Publish," and follow the wizard. Choose "Azure" as the target and "Azure App Service" as the specific target. Select your created App Service or create a new one.

Option B: Using Azure CLI (Zip Deploy)

Build your application for release:

dotnet publish -c Release -o ./publish

Zip the contents of the publish folder:

cd publish
zip -r ../app.zip *
cd ..

Deploy using the Azure CLI:

az webapp deploy --resource-group MyResourceGroup --name MyAspNetCoreApp --src-path app.zip --type zip

Option C: Using GitHub Actions or Azure DevOps Pipelines

For CI/CD, integrate your deployment pipeline with Azure App Service. This is the recommended approach for production deployments.

Step 4: Configure Application Settings

You might need to configure application settings, connection strings, or environment variables in your App Service.

In the Azure portal, navigate to your App Service and go to "Configuration" under "Settings." You can add application settings here.

For example, to set the .NET Core environment to Production:

ASPNETCORE_ENVIRONMENT: Production

Step 5: Verify Your Deployment

After deployment, you can browse to your App Service URL (e.g., https://myaspnetcoreapp.azurewebsites.net) to see your application live.

Troubleshooting

If you encounter issues:

Deploying ASP.NET Core apps to Azure App Service is a straightforward process that offers scalability, reliability, and ease of management. Happy coding!