title | description | ms.date | author | ms.author | ms.topic |
---|---|---|---|---|---|
Deploy to Azure from Visual Studio |
This tutorial will walk you through building and deploying a Microsoft Azure application using Visual Studio and .NET. |
06/20/2017 |
CamSoper |
casoper |
quickstart |
This tutorial will walk you through building and deploying a Microsoft Azure application using Visual Studio and .NET. When finished, you'll have a web-based to-do application built in ASP.NET MVC Core, hosted as an Azure Web App, and using Azure Cosmos DB for data storage.
Azure Cosmos DB is used for data storage in this tutorial, so you'll need to create an account. Run this script locally or in the Cloud Shell to create an Azure Cosmos DB SQL API account. Click the Try it button on the code block below to launch the Azure Cloud Shell and copy/paste the script block into the shell.
# Create the DotNetAzureTutorial resource group
az group create --name DotNetAzureTutorial --location EastUS
# Generate a unique name for the account
let randomNum=$RANDOM*$RANDOM
cosmosdbname=dotnettutorial$randomNum
# Create the Azure Cosmos DB account
az cosmosdb create --name $cosmosdbname --resource-group DotNetAzureTutorial
# Retrieve the endpoint and key (you'll need these later)
cosmosEndpoint=$(az cosmosdb show -n $cosmosdbname -g DotNetAzureTutorial --query [documentEndpoint] -o tsv)
cosmosAuthKey=$(az cosmosdb list-keys -n $cosmosdbname -g DotNetAzureTutorial --query [primaryMasterKey] -o tsv)
printf "\n\nauthKey: $cosmosAuthKey\nendpoint: $cosmosEndpoint\n\n"
# Done!
Make a note of the displayed authKey and endpoint
Let's get the sample code for this walkthrough and hook it up to your Azure Cosmos DB account.
-
Download the sample code. You can get it from GitHub, or if you have the git command line client, clone it to your local machine with the following command:
git clone https://github.com/Azure-Samples/dotnet-cosmosdb-quickstart
-
Open todo.csproj in Visual Studio.
-
Open appsettings.json in the web project. Look for the following lines:
"authKey": "AUTHKEYVALUE", "endpoint": "ENDPOINTVALUE",
Replace AUTHKEYVALUE and ENDPOINTVALUE with the values you noted earlier.
-
Press F5 to restore the project's NuGet packages, build the project, and run it locally.
The web application should run locally in your browser. You can add new items to the to-do list by clicking Create New. Note the data you enter in the application is being stored in your Azure Cosmos DB account. You can view your data in the Azure portal by selecting Azure Cosmos DB from the left menu, selecting your account, and then selecting Data Explorer.
You've successfully built an application that uses Azure services like Azure Cosmos DB. Next, we'll deploy our web application to the cloud.
Important
Be sure you're signed into Visual Studio with the same account your Azure subscription is associated with.
-
In Visual Studio Solution Explorer, right-click on the project name and select Publish...
-
Using the Publish dialog, select Microsoft Azure App Service, select Create New, and then click Publish
-
Complete the Create App Service dialog as follows:
- Enter a unique Web App Name. This will be part of the URL for your app.
- Select the Azure Subscription you're deploying to. Use same subscription with which you were logged into Cloud Shell earlier.
- Select DotNetAzureTutorial for the Resource Group for your web application.
- Select or create an App Service Plan to determine the pricing your your application. Here's more information about App Service Plans.
-
Click Create to deploy the application. When deployment is complete, a browser will open with your deployed application.
When you're done testing the app and inspecting the code and resources, you can delete the Web App and Azure Cosmos DB account by deleting the resource group in the Cloud Shell.
az group delete -n DotNetAzureTutorial