AspNetCore.Docs/aspnetcore/tutorials/publish-to-azure-webapp-usi...

182 lines
6.7 KiB
Markdown
Raw Normal View History

2016-10-29 01:35:15 +08:00
---
2017-07-01 07:47:15 +08:00
title: Publish an ASP.NET Core app to Azure using Visual Studio
2016-10-29 01:35:15 +08:00
author: rick-anderson
2016-11-18 04:13:02 +08:00
description:
keywords: ASP.NET Core
2016-10-29 01:35:15 +08:00
ms.author: riande
manager: wpickett
ms.date: 10/05/2017
ms.topic: get-started-article
2016-10-29 01:35:15 +08:00
ms.assetid: 78571e4a-a143-452d-9cf2-0860f85972e6
2016-11-17 08:24:57 +08:00
ms.technology: aspnet
ms.prod: asp.net-core
2016-10-29 01:35:15 +08:00
uid: tutorials/publish-to-azure-webapp-using-vs
---
2017-04-08 04:11:19 +08:00
# Publish an ASP.NET Core web app to Azure App Service using Visual Studio
2016-10-29 01:35:15 +08:00
By [Rick Anderson](https://twitter.com/RickAndMSFT), [Cesar Blum Silveira](https://github.com/cesarbs), and [Rachel Appel](https://twitter.com/rachelappel)
2016-10-29 01:35:15 +08:00
## Set up
2016-10-29 01:35:15 +08:00
* Open a [free Azure account](https://aka.ms/K5y5yh) if you do not have one.
2016-10-29 01:35:15 +08:00
## Create a web app
In the Visual Studio Start Page, select **File > New > Project...**
2016-10-29 01:35:15 +08:00
![File menu](publish-to-azure-webapp-using-vs/_static/file_new_project.png)
2016-10-29 01:35:15 +08:00
Complete the **New Project** dialog:
* In the left pane, select **.NET Core**.
2016-10-29 01:35:15 +08:00
* In the center pane, select **ASP.NET Core Web Application**.
2016-10-29 01:35:15 +08:00
* Select **OK**.
2016-10-29 01:35:15 +08:00
![New Project dialog](publish-to-azure-webapp-using-vs/_static/new_prj.png)
2016-10-29 01:35:15 +08:00
In the **New ASP.NET Core Web Application** dialog:
2016-10-29 01:35:15 +08:00
* Select **Web Application**.
2016-10-29 01:35:15 +08:00
* Select **Change Authentication**.
2016-10-29 01:35:15 +08:00
![New Project dialog](publish-to-azure-webapp-using-vs/_static/new_prj_2.png)
2016-10-29 01:35:15 +08:00
The **Change Authentication** dialog appears.
2016-10-29 01:35:15 +08:00
* Select **Individual User Accounts**.
2016-10-29 01:35:15 +08:00
* Select **OK** to return to the **New ASP.NET Core Web Application**, then select **OK** again.
2016-10-29 01:35:15 +08:00
![New ASP.NET Core Web authentication dialog](publish-to-azure-webapp-using-vs/_static/new_prj_auth.png)
2016-10-29 01:35:15 +08:00
Visual Studio creates the solution.
2016-10-29 01:35:15 +08:00
## Run the app locally
2016-10-29 01:35:15 +08:00
* Choose **Debug** then **Start Without Debugging** to run the app locally.
2016-10-29 01:35:15 +08:00
* Click the **About** and **Contact** links to verify the web application works.
2016-10-29 01:35:15 +08:00
![Web application open in Microsoft Edge on localhost](publish-to-azure-webapp-using-vs/_static/show.png)
2016-10-29 01:35:15 +08:00
* Select **Register** and register a new user. You can use a fictitious email address. When you submit, the page displays the following error:
2016-10-29 01:35:15 +08:00
*"Internal Server Error: A database operation failed while processing the request. SQL exception: Cannot open the database. Applying existing migrations for Application DB context may resolve this issue."*
2016-10-29 01:35:15 +08:00
* Select **Apply Migrations** and, once the page updates, refresh the page.
2016-10-29 01:35:15 +08:00
![Internal Server Error: A database operation failed while processing the request. SQL exception: Cannot open the database. Applying existing migrations for Application DB context may resolve this issue.](publish-to-azure-webapp-using-vs/_static/mig.png)
2016-10-29 01:35:15 +08:00
The app displays the email used to register the new user and a **Log out** link.
2016-10-29 01:35:15 +08:00
![Web application open in Microsoft Edge. The Register link is replaced by the text Hello email@domain.com!](publish-to-azure-webapp-using-vs/_static/hello.png)
2016-10-29 01:35:15 +08:00
## Deploy the app to Azure
Close the web page, return to Visual Studio, and select **Stop Debugging** from the **Debug** menu.
2016-10-29 01:35:15 +08:00
Right-click on the project in Solution Explorer and select **Publish...**.
![Contextual menu open with Publish link highlighted](publish-to-azure-webapp-using-vs/_static/pub.png)
2016-10-29 01:35:15 +08:00
In the **Publish** dialog, select **Microsoft Azure App Service** and click **Publish**.
2016-10-29 01:35:15 +08:00
![Publish dialog](publish-to-azure-webapp-using-vs/_static/maas1.png)
2016-10-29 01:35:15 +08:00
* Name the app a unique name.
2016-10-29 01:35:15 +08:00
2017-09-13 23:06:52 +08:00
* Select a subscription.
2016-10-29 01:35:15 +08:00
* Select **New...** for the resource group and enter a name for the new resource group.
2016-10-29 01:35:15 +08:00
* Select **New...** for the app service plan and select a location near you. You can keep the name that is generated by default.
2016-10-29 01:35:15 +08:00
![App Service dialog](publish-to-azure-webapp-using-vs/_static/newrg1.png)
2016-10-29 01:35:15 +08:00
* Select the **Services** tab to create a new database.
2016-10-29 01:35:15 +08:00
* Select the green **+** icon to create a new SQL Database
2016-10-29 01:35:15 +08:00
![New SQL Database](publish-to-azure-webapp-using-vs/_static/sql.png)
2016-10-29 01:35:15 +08:00
* Select **New...** on the **Configure SQL Database** dialog to create a new database.
2016-10-29 01:35:15 +08:00
![New SQL Database and server](publish-to-azure-webapp-using-vs/_static/conf.png)
2016-10-29 01:35:15 +08:00
The **Configure SQL Server** dialog appears.
2016-10-29 01:35:15 +08:00
* Enter an administrator user name and password, and then select **OK**. Don't forget the user name and password you create in this step. You can keep the default **Server Name**.
2016-10-29 01:35:15 +08:00
* Enter names for the database and connection string.
2016-10-29 01:35:15 +08:00
> [!NOTE]
> "admin" is not allowed as the administrator user name.
![Configure SQL Server dialog](publish-to-azure-webapp-using-vs/_static/conf_servername.png)
2016-10-29 01:35:15 +08:00
* Select **OK**.
2016-10-29 01:35:15 +08:00
Visual Studio returns to the **Create App Service** dialog.
2016-10-29 01:35:15 +08:00
* Select **Create** on the **Create App Service** dialog.
2016-10-29 01:35:15 +08:00
![Configure SQL Database dialog](publish-to-azure-webapp-using-vs/_static/conf_final.png)
* Click the **Settings** link in the **Publish** dialog.
2016-10-29 01:35:15 +08:00
![Publish dialog: Connection panel](publish-to-azure-webapp-using-vs/_static/pubc.png)
2016-10-29 01:35:15 +08:00
On the **Settings** page of the **Publish** dialog:
2016-10-29 01:35:15 +08:00
* Expand **Databases** and check **Use this connection string at runtime**.
2016-10-29 01:35:15 +08:00
* Expand **Entity Framework Migrations** and check **Apply this migration on publish**.
2016-10-29 01:35:15 +08:00
* Select **Save**. Visual Studio returns to the **Publish** dialog.
2016-10-29 01:35:15 +08:00
![Publish dialog: Settings panel](publish-to-azure-webapp-using-vs/_static/pubs.png)
2016-10-29 01:35:15 +08:00
Click **Publish**. Visual Studio will publish your app to Azure and launch the cloud app in your browser.
2016-10-29 01:35:15 +08:00
### Test your app in Azure
* Test the **About** and **Contact** links
* Register a new user
![Web application opened in Microsoft Edge on Azure App Service](publish-to-azure-webapp-using-vs/_static/register.png)
2016-10-29 01:35:15 +08:00
### Update the app
* Edit the *Pages/About.cshtml* Razor page and change its contents. For example, you can modify the paragraph to say "Hello ASP.NET Core!":
2016-10-29 01:35:15 +08:00
[!code-html[About](publish-to-azure-webapp-using-vs/sample/about.cshtml?highlight=9&range=1-9)]
2016-10-29 01:35:15 +08:00
* Right-click on the project and select **Publish...** again.
2016-10-29 01:35:15 +08:00
![Contextual menu open with Publish link highlighted](publish-to-azure-webapp-using-vs/_static/pub.png)
2016-10-29 01:35:15 +08:00
* After the app is published, verify the changes you made are available on Azure.
![Verify task is complete](publish-to-azure-webapp-using-vs/_static/final.png)
2016-10-29 01:35:15 +08:00
### Clean up
When you have finished testing the app, go to the [Azure portal](https://portal.azure.com/) and delete the app.
* Select **Resource groups**, then select the resource group you created.
2016-10-29 01:35:15 +08:00
![Azure Portal: Resource Groups in sidebar menu](publish-to-azure-webapp-using-vs/_static/portalrg.png)
2016-10-29 01:35:15 +08:00
* In the **Resource groups** page, select **Delete**.
2016-10-29 01:35:15 +08:00
![Azure Portal: Resource Groups page](publish-to-azure-webapp-using-vs/_static/rgd.png)
2016-10-29 01:35:15 +08:00
* Enter the name of the resource group and select **Delete**. Your app and all other resources created in this tutorial are now deleted from Azure.
2016-10-29 01:35:15 +08:00
### Next steps
* [Continuous Deployment to Azure with Visual Studio and Git](../publishing/azure-continuous-deployment.md)