DEV Community

Cover image for Deploy a .NET 6 Web Application to an Azure App Service by the command line
Emanuele Bartolesi
Emanuele Bartolesi

Posted on

Deploy a .NET 6 Web Application to an Azure App Service by the command line

CI/CD is a good approach to deploy web applications (and not only web applications) but there a lot of reasons for not configuring an entire workflow from the beginning of the project (disclaimer: I am a big fan of CI/CD or GitHub Actions or Azure DevOps).

We will use the Azure Command-Line Interface (Az CLI) to create and manage resources on Azure.
If you don't have the Az CLI installed on your machine, you can follow the steps from the official documentation to install it.
It's very easy and you can use the CLI from Windows, Mac or Linux.
This is one of the reasons why I prefer the Az CLI instead of PowerShell.
Installation Guide

After the installation you can login on your Azure Tenant with the command:

az login
Enter fullscreen mode Exit fullscreen mode

Publish and Zip the application

First of all, we need to publish the application and create a zip file with all the binaries.
Let's do that with the following commands:

dotnet publish -o publish
cd .\publish
Compress-Archive . publish.zip
Enter fullscreen mode Exit fullscreen mode

Compress-Archive is a PowerShell command. If you are using the Az CLI from another OS, you can find the right way to create a zip file for folder from the command line.

Execute the deployment

Now you are ready to deploy the web application with the zip deploy.
From the publish folder, you can launch the following script:

az webapp deployment source config-zip --src .\publish.zip -n "appServiceName" -g "resourceGroupName"
Enter fullscreen mode Exit fullscreen mode

Remember to replace the appServiceName and the resourceGroupName placeholders with your right values for these two parameters.
If you are using PowerShell you can also create two variables before that line. It's more clear and easier to maintain.

$appServiceName = "app_service_name"
$resourceGroupName = "resource_group_name"

az webapp deployment source config-zip --src .\publish.zip -n $appServiceName -g $resourceGroupName

Enter fullscreen mode Exit fullscreen mode

If everything works, you see the result of the activities after a while, directly in the console.

az CLI deploy

Top comments (3)

Collapse
 
klimcio profile image
Mariusz

What to do if everything works as described and I end up with "You do not have permission to view this directory or page." when opening the webpage?

Collapse
 
kasuken profile image
Emanuele Bartolesi

Do you compress the content of the publish folder and not the parent folder?

Collapse
 
klimcio profile image
Mariusz • Edited

I do run the Compress cmdlet while in the publish folder. I end up having a publish folder in the wwwroot folder on my Azure resource.

Edit:
Found the solution. While in the publish folder the cmdlet that worked would be: Compress-Archive * publish.zip