MSDN Documentation

Deploying ASP.NET Core Applications

This tutorial guides you through the process of deploying your ASP.NET Core applications to various environments, ensuring your web applications are accessible and performant.

Introduction

Deploying an ASP.NET Core application involves several key steps, from preparing your application for release to configuring the target environment. We'll cover common deployment targets like IIS, Docker, and Azure App Service.

1. Preparing Your Application

Before deployment, it's crucial to publish your application in a release configuration. This optimizes the code for performance and reduces the application's size.

Use the following .NET CLI command:

dotnet publish -c Release -o ./publish_output

The -c Release flag specifies the Release build configuration, and -o ./publish_output directs the output to a directory named publish_output.

Tip: For self-contained deployments (including the .NET runtime), use the --runtime flag. For example, to target Windows x64:
dotnet publish -c Release --runtime win-x64 -o ./publish_output

2. Deployment to Internet Information Services (IIS)

IIS is a popular web server for Windows environments. To deploy an ASP.NET Core application to IIS, you'll need to install the ASP.NET Core Hosting Bundle.

3. Deployment with Docker

Docker provides a containerized environment for your application, ensuring consistency across different platforms.

Create a Dockerfile in your project's root directory:


FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["YourApp.csproj", "YourApp/"]
COPY ["Controllers/","YourApp/Controllers/"]
COPY ["Models/","YourApp/Models/"]
COPY ["Pages/","YourApp/Pages/"]
COPY ["wwwroot/","YourApp/wwwroot/"]
RUN dotnet restore "YourApp/YourApp.csproj"
COPY . "YourApp/"
WORKDIR "/src/YourApp"
RUN dotnet build "YourApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "YourApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "YourApp.dll"]
            

Build the Docker image:

docker build -t your-app-image .

Run the Docker container:

docker run -d -p 8080:80 your-app-image
Note: Replace YourApp with the actual name of your project and adjust the paths in the Dockerfile accordingly. Ensure you are using the correct .NET SDK and Runtime version.

4. Deployment to Azure App Service

Azure App Service is a fully managed platform for hosting web applications.

5. Configuration and Best Practices

Conclusion

Deploying ASP.NET Core applications can be straightforward with the right tools and understanding. This tutorial covered common scenarios, but always refer to specific platform documentation for advanced configurations and optimizations.