Azure Deploy Asp.NET Core Web App

In the era of cloud computing, when you deploy a website, the first choice must be various cloud services. So what kind of cloud service can be used to deploy an ASP.NET Core website in the fastest way? Azure's Web App service is a good choice.

Next, we will create an Asp.net Core demo application through Visual Studio, and then deploy the demo application to Azure Web App.

By reading this article, you will learn how to create an Asp.NET Core application and how to create a Web App on Azure and deploy an Asp.NET Core application.

What is Azure Web App?

A Web App is a computing resource provided by Azure to run websites and web applications. If you want a simple analogy, you can understand it as a virtual directory under IIS. As long as we upload the Web application to the designated place, it can provide Web services.

Azure Web App has many advantages, but I want to emphasize only one here: Simple!

 

Create an Asp.NET Core app

If you want to create an Asp.net Core application through Visual Studio, you need to install .net core vs tool first. For details, please refer to here . Let's first create an Asp.net Core demo application.

Open a new project in Visual Studio and select the Web->ASP.NET Core Web Application (.NET Core) template.

Select the "Web Application" template in the New ASP.NET Core Web Application (.NET Core) dialog. Then modify the Authentication type to "Individual User Accounts". At this point, the application created from the template will contain the module for user management. Also uncheck the "Host in the cloud" option, then click the OK button.

Even if the demo program is created, it still needs a little bit of setting to make it run correctly.

Press F5 to run the demo application locally, click "Register" and register a new user. When you fill in the information and press the "Register" button you will get the following error:

The general meaning is that the database is not set up properly, and the solutions are also mentioned above. There are three kinds:

  1. Just click the "Apply Migrations" button.
  2. Execute the "Update-Database" command in Visual Studio's Package Manager Console.
  3. Execute the dotnet ef database update command.

Here choose the second way:

 

Then restart the site and register a new user, all OK!

At this point, the creation of the Asp.NET Core web application is complete.

 

Create an Azure Web App

Azure Web App services can be created in different ways, such as directly on Azure Websites. You can also create an Azure Web App directly in the process of publishing the app through Visual Studio. Below we will demonstrate the detailed steps to create an Azure Web App using Visual Studio.

Right-click the created project name and select "Publish".

In this step, you need to enter your MS subscription account (all resources on Azure are managed through your account). After logging in, click the new button to start the creation process:

默认的 Service 类型就是 Web App,所以不需要再进行配置。下面选择或者创建 Resource Group/App Service Plan。这里稍微解释一下,Resource Group 是资源管理的逻辑单位,可以通过 Resource Group 管理其包含的资源,主要是进行授权。App Service Plan 简单点说就是管理你的资源分配,你花费一块钱和十块钱显然是会分配到不同的资源。然后点击“Explore additional Azure services”开始创建数据库。

我们的应用中用到了数据库,所以需要在 Azure 上创建数据库。现在进入数据库创建流程,点击右侧绿色的加号:

数据库的创建包含两部分。首先要创建 SQL Server 数据库服务器,然后在数据库服务器上创建数据库实例。相关概念可以参考笔者的《迁移 SQL Server 数据库到 Azure SQL 实战》一文,此处不再赘述。

因此在创建数据库时,需要先创建数据库服务器:

下面是创建新的数据库服务器的界面,请输入合适的数据库服务器名称,并设置好你的用户名和密码,然后点击 OK 按钮:

接下来又回到了创建数据库的界面,此时的数据库服务器已经设置好了,只要再输入数据库的名称就可以了。然后点击 OK 按钮进入下一步:

下面是添加完数据库配置的样子,直接点 Create 按钮完成所有配置的创建:

至此,我们已经完成了 Azure Web App 和云端数据库的创建。接下来看我们如何把 Asp.NET Core 应用发布到 Azure Web App 上去。

 

部署 Asp.NET Core 应用

在完成了 Azure Web App 的创建后,我们回到部署过程。在 Connection 选项卡中,其实我们看到的设置都是默认值,没有特殊要求的话是不需要修改的。点击进入 Settings 选项卡:

 

基本的配置使用默认值就可以了。我们需要选择使用数据库的连接字符串,和 Apply this migration on publish 就可以了。然后点击 Publish 按钮完成往云端的发布:

根据你的网络状况,发布过程可能会持续上一小会儿。当发布完成后,你就可以在浏览器里查看应用了。

 

简单的测试

我们的应用有一个很重要的功能:可以创建用户并且登录。在完成发布后,你一定很想测试一下这个功能,因为我们并没有为创建这个功能做过什么。

下面试着创建一个新的用户 [email protected]

 

还不错,创建完成后,用户 [email protected] 已经成功登录了。

总结

本文通过详细的操作步骤向大家介绍了 Asp.NET Core 应用的创建并且向 Azure Web App Service 上部署的过程。

希望对有兴趣的同学有所帮助。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326228390&siteId=291194637