Deploying a Web API and an Angular App to Azure
Put the products app on the internet. The database moves to Azure SQL Database, the built Angular app goes into the API's wwwroot so one App Service serves both from one address, and the connection string and signing key become App Service settings, never files in Git.
Journeyman · ASP.NET Core · Angular · Updated
After this lesson you can
- Serve a built Angular app from an ASP.NET Core API, so both share one address
- Deploy the API to Azure App Service, with its database in Azure SQL and its secrets in the app's settings
Key takeaways
ng buildmakes the production app; copy it into the API'swwwrootso one App Service serves both from one address.MapFallbackToFile("index.html")makes deep links such as/products/newwork, by answering any other address with the Angular app.MapFallback("/api/{**rest}", ...)keeps a mistyped API address, such as/api/prodcts, answering404instead of sending the Angular app.- The connection string and a new JWT key, never your development one, go in the App Service's settings, not in files.
Jwt__Keyreaches the code asJwt:Key. - Delete the resource group when you've finished, and everything in it goes, so nothing can cost money later.
Your app runs on your laptop. To let anyone use it, three things move to Azure, Microsoft's cloud:
| On your laptop | In Azure |
|---|---|
| SQL Server, with your Northwind database | Azure SQL Database |
| The API, run from Visual Studio | Azure App Service: a web server Microsoft runs for you |
The Angular app, from ng serve |
Built into files, and served by the same App Service |
| User secrets | The App Service's settings |
One address for both
On your laptop, the Angular dev server's proxy sends /api requests to the API, so the browser sees one address. On the internet there's no dev server. The simplest way to keep one address: let the API serve the Angular app too. You did this for a plain page in Calling APIs from JavaScript, with wwwroot.
1. Build the Angular app
In the Angular project's folder:
ng build
ng build makes the production version of the app, small and fast, in dist/northwind-client/browser (with your project's name in place of northwind-client): an index.html, some .js files and a .css file. That's the whole app; no Node.js is needed to run it.
Copy everything in dist/northwind-client/browser into the API project's wwwroot folder (make the folder if there isn't one).
2. Let the API serve it
In the API's Program.cs:
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapFallback("/api/{**rest}", () => Results.NotFound());
app.MapFallbackToFile("index.html");
app.Run();
UseDefaultFilesandUseStaticFilesserve the files inwwwroot:/getsindex.html, and the.jsand.cssfiles come with it.MapFallbackToFile("index.html")answers every other address withindex.html. That's what makes deep links work: someone opens/products/newfrom a bookmark, the server has no such file, so it sends the Angular app, and Angular's router shows the right page.MapFallback("/api/{**rest}", ...)catches any address under/apithat no controller answered. It's more specific than theindex.htmlfallback, so it wins, and a mistyped API address, such as/api/prodcts, still answers404, rather than an HTML page that would confuse the app.
Run the API, and open its address: the Angular app appears, from the API itself, and its /api requests go to the same place. That's exactly what will happen in Azure.
The database: Azure SQL Database
In the Azure portal (https://portal.azure.com), sign in, or make a free account. Students can often get free credit through Azure for Students.
- Make a resource group, such as
northwind-rg: a folder that holds everything for this app, so you can see it, and delete it, together. - Create an Azure SQL Database, called
Northwind, in that resource group. Azure offers a free amount of Azure SQL Database every month: choose the free offer if the page shows it. You'll also make a server, such asnorthwind-yourname, with a SQL admin login and password. Keep them safe. - On the server's Networking page, allow Azure services to reach the server, so your App Service can, and add your own IP address, so you can connect from SSMS.
- In SSMS, connect to
northwind-yourname.database.windows.netwith the SQL login. Before you click Connect, click Options, and on Connection Properties set Connect to database toNorthwind. - Open the Northwind script, and delete four lines near its top, each with the
GOafter it:USE master,DROP DATABASE IF EXISTS Northwind,CREATE DATABASE Northwindanduse Northwind. Azure already made the database in step 2, and Azure SQL can't switch databases withUSE, so those lines would fail, or make a second database that isn't free. Keep theSETlines. Run the script, then runUsers.sql.
The database's Connection strings page shows the connection string for ADO.NET. It has {your_password} in it: you'll put the real password in when you paste it into the App Service, never into a file.
The API: Azure App Service
- Create a Web App in the same resource group. Choose:
- a unique name, such as
northwind-yourname, which becomeshttps://northwind-yourname.azurewebsites.net; - Code, with the runtime stack .NET 10, on Linux;
- the Free (F1) pricing plan, while you're learning.
- a unique name, such as
- In the Web App, open Settings → Environment variables:
- Under Connection strings, add one named
Northwind, of type SQLAzure, with the connection string from the database, and your password in it. - Under App settings, add
Jwt__Key, with a new key made with yourkey.csprogram from Securing a Web API with JWT Bearer. Never reuse your development key.
- Under Connection strings, add one named
The double underscore in Jwt__Key stands for the colon in Jwt:Key: settings like these reach your app as environment variables, where a colon isn't allowed. ASP.NET Core reads them as if they were in appsettings.json, so builder.Configuration["Jwt:Key"] and GetConnectionString("Northwind") find them, and the code doesn't change at all.
Publish
In Visual Studio, right-click the API project, and choose Publish. Pick Azure, then Azure App Service (Linux), sign in, and choose your Web App. Then click Publish. Visual Studio builds the API, with everything in wwwroot, uploads it, and opens the site in the browser.
To publish a new version, build the Angular app again if it changed, copy it into wwwroot, and click Publish again.
Try it
Open https://northwind-yourname.azurewebsites.net:
- The products load: from Azure SQL, through the API, into the Angular app, all from one address.
- Open
https://northwind-yourname.azurewebsites.net/products/newdirectly: you're sent to sign in, so deep links work. - In Insomnia, change the
Northwind API (secured)collection'sbase_urlto your Azure address, register a user with request 11, and sign in on the site. Every request in the collection now goes to Azure: that's what keeping the address in one place is for.
If the site shows an error, the Web App's Log stream page shows what the API writes as it starts and runs. The most common causes are a typo in a setting's name, or the database's networking not allowing Azure services.
Costs, and cleaning up
The free tiers are meant for learning: a Free App Service plan has a daily limit on CPU time and goes to sleep when nobody uses it, so the first visit after a while is slow. A real business app would use a paid plan. Check Azure's pricing pages before you choose one: prices change.
When you've finished experimenting, delete the resource group, and everything in it goes, so nothing can cost money later.
As apps grow, teams move on to separate hosting for the front end, such as Azure Static Web Apps, and to GitHub Actions, which build and publish every time code is pushed. One App Service serving both is the simplest way to start.
Check yourself
2 questions to try on the lesson page.
So what
Build the Angular app into the API's wwwroot, so one App Service serves both from one address. Move the database to Azure SQL, and the connection string and key into the App Service's settings. Then publish, and the app is live.
Key terms
- Azure SQL Database
- App Service
- Resource Group
- Environment Variables in .NET
- ng build