This is part 31 of azure tutorial. In this video, we'll discuss how to deploy ASP.NET
Core Web API along with a SQL server database to Azure App Service right from within Visual Studio.
Deploying a web API is very similar to deploying a web application. Let's take a look
at the steps. In visual studio, in the solution explorer, right click on your web API project
and then select this option - Publish. We want to publish to azure so select "Azure" as the target.
Click Next. On this screen select Azure App Service and then click Next. At the moment in
azure, we don't have any app service instances and that's the reason we don't see anything
within this app service instances box. Now, we want to create a new instance of app service
to which we want to deploy this web API and for that, click on this + sign right here.
Provide a meaningful name for the app service instance, I'm going to name it EmployeeMgmtApi
and then select your "Subscription" from this drop-down list and then a resource group. I'm
actually going to create a new resource group. Let me name it rgApi, for resource group API.
We want to start free in azure, so let's create a new hosting plan. Provide a name for the plan,
select the region where you want this plan to be created. I'm actually going to select East US
and then the size for your plan. We want to start free, so let's select that and in the
future if we want to add more features we can always scale this plan up. So, let's click OK
and then Create. At the moment,
an app service instance is being created in azure. It's going to take a few seconds.
There we go, we have our app service instance created and notice by default, this new instance
is already selected, meaning we are going to deploy our API to this app service instance.
So, let's click Next. On this screen, it's asking us if we want to create an instance
of API Management. We'll discuss what is azure API management and the benefits it provides in
our upcoming videos. For now, let's skip this step by selecting this checkbox and then click
Finish. At this point, we have a new instance of app service created in azure and we have
configured visual studio to deploy to that app service instance and remember, for our API
the data comes from a SQL server database, so our web API has a dependency on a SQL server database
and visual studio is smart enough to detect that dependency. Notice, under "Service Dependencies"
visual studio is showing there is a dependency on a SQL server database and it's asking us
if we want to configure it. So, our next step is to create a SQL database
in azure. For that, click "Configure" and then select "Azure Sql Database", click Next.
At the moment in azure, we don't have any SQL server instance. First we need to create a
SQL server instance before we can create a SQL database. So, for that click on this "New" link
next to database server. Provide a name for the database server. We have a validation
error at the moment and that is because we have a dot in the server name here,
not sure why it included it in the first place and then a location, administrator username, password,
confirm password and then click OK and then Create.
At the moment, both SQL server instance and the database are being created in azure.
There we go, both the server and the database are created. Let's click Next. On this screen,
provide the database connection username and the password and then click Next and Finish,
finally Close. Now, so far we have done two things, created a new instance of app service
and a SQL database in azure. Before we can deploy this API in azure,
we need to do one more little thing, remember, the data for our API comes from a SQL database
and we want to tell visual studio to execute these database migrations when we deploy this
API in azure and to do that, click on this "Edit" link here. On this "Publish" dialog,
click on this "Settings" tab and then expand this section "Entry framework migrations"
and check this checkbox here - Apply this migration on publish and then click "Save".
We are almost there, all that is left right now to do is, click this "Publish" button.
There we go, our API deployment is in progress, it's going to take a few seconds to complete.
There we go, our API deployment is complete and at the moment we see 404 error and that
kind of makes sense because at this root URL, we don't have anything to display. Now, if we want
to see the list of employees then navigate to this endpoint /api/employees and we see the list of all
employees. If we want a specific employee simply pass the id and similarly if we want the list of
all departments api/departments. In our previous two videos in the series, we discussed what is
swagger and how to use it in a .Net core project. So, if we navigate to this endpoint, we should see
the OpenAPI specification document for our Employee Management API, there we go, we see
the OpenApi specification document and remember it is this open api specification document
that drives the documentation of our API, and to see the documentation we just navigate to our root
application url /swagger. There we go, we see our API documentation as expected. Now, here's
what we want to do, when we navigate to our root application URL, at the moment we see 404 error,
instead we want our API documentation to be available at this root URL and for that,
we need to do a small change in startup.cs file of our API project, basically we are telling
with this change, we don't want to use any route prefix. Now, let's deploy our changes to azure.
There we go, at the root, we now have our API documentation which makes more
sense than displaying 404 error. That's it in this video. Thank you for listening.