The Azure Code Samples are currently available in English

Manage your web apps with the .NET SDK for Azure

Poslední aktualizace: 12.08.2016
Upravit na GitHubu

This sample demonstrates how to manage your WebApps using the .NET SDK

On this page

Run this sample

  1. If you don't have it, install the .NET Core SDK.

  2. Clone the repository.

    git clone
  3. Install the dependencies.

    dotnet restore
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Add these environment variables to your .env file using your subscription id and the tenant id, client id and client secret from the service principle that you created.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client or application id}
    export AZURE_CLIENT_SECRET={your service principal secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
  6. Run the sample.

    dotnet run

What does Program.cs do?

Main() gets the environment variables that you set up for this sample and calls RunSample. RunSample starts by setting up a WebSiteManagementClient using those credentials.

// Build the service credentials and Azure Resource Manager clients
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var resourceClient = new ResourceManagementClient(serviceCreds);
resourceClient.SubscriptionId = subscriptionId;
var webClient = new WebSiteManagementClient(serviceCreds);
webClient.SubscriptionId = subscriptionId;

The sample then sets up a resource group in which it will create the website.

Random r = new Random();
int postfix = r.Next(0, 1000000);

var resourceGroupName = "sample-dotnet-app-service-group";
var westus = "westus";
var serverFarmName = "sample-server-farm";
var siteName = "sample-site-name-" + postfix;

// Create the resource group
Write("Creating resource group: {0}", westus);
resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup { Location = westus});

Create a server farm

Create a server farm to host the website.

var serverFarm = webClient.ServerFarms.CreateOrUpdateServerFarm(resourceGroupName, serverFarmName, new ServerFarmWithRichSku{
    Location = westus,
    Sku = new SkuDescription{
        Name = "S1",
        Capacity = 1,
        Tier = "Standard"

Create a website

var site = webClient.Sites.CreateOrUpdateSite(resourceGroupName, siteName, new Site{
    Location = westus,
    ServerFarmId = serverFarm.Id

Get details for the given website

var gotSite = webClient.Sites.GetSite(resourceGroupName, siteName);

Delete a website

webClient.Sites.DeleteSite(resourceGroupName, siteName);

More information

Please refer to Azure SDK for .NET for more information.