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 build makes the production app; copy it into the API's wwwroot so one App Service serves both from one address.
  • MapFallbackToFile("index.html") makes deep links such as /products/new work, by answering any other address with the Angular app.
  • MapFallback("/api/{**rest}", ...) keeps a mistyped API address, such as /api/prodcts, answering 404 instead 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__Key reaches the code as Jwt: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();
  • UseDefaultFiles and UseStaticFiles serve the files in wwwroot: / gets index.html, and the .js and .css files come with it.
  • MapFallbackToFile("index.html") answers every other address with index.html. That's what makes deep links work: someone opens /products/new from 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 /api that no controller answered. It's more specific than the index.html fallback, so it wins, and a mistyped API address, such as /api/prodcts, still answers 404, 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.

  1. 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.
  2. 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 as northwind-yourname, with a SQL admin login and password. Keep them safe.
  3. 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.
  4. In SSMS, connect to northwind-yourname.database.windows.net with the SQL login. Before you click Connect, click Options, and on Connection Properties set Connect to database to Northwind.
  5. Open the Northwind script, and delete four lines near its top, each with the GO after it: USE master, DROP DATABASE IF EXISTS Northwind, CREATE DATABASE Northwind and use Northwind. Azure already made the database in step 2, and Azure SQL can't switch databases with USE, so those lines would fail, or make a second database that isn't free. Keep the SET lines. Run the script, then run Users.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

  1. Create a Web App in the same resource group. Choose:
    • a unique name, such as northwind-yourname, which becomes https://northwind-yourname.azurewebsites.net;
    • Code, with the runtime stack .NET 10, on Linux;
    • the Free (F1) pricing plan, while you're learning.
  2. 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 your key.cs program from Securing a Web API with JWT Bearer. Never reuse your development key.

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:

  1. The products load: from Azure SQL, through the API, into the Angular app, all from one address.
  2. Open https://northwind-yourname.azurewebsites.net/products/new directly: you're sent to sign in, so deep links work.
  3. In Insomnia, change the Northwind API (secured) collection's base_url to 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

Cheat sheets