The updated Angular project template in Visual Studio 2019 (and 2017 before that) provides a convenient starting point for ASP.NET Core apps using Angular and the Angular CLI to implement a rich, client-side user interface (UI). The template is equivalent to creating an ASP.NET Core project to act as an API backend and an Angular CLI project to act as a UI.
Whereas the template offers the convenience of hosting both project types in a single app project, I highly recommend that the app project doesn’t get published as a single unit. Instead, I recommended that the API backend gets hosted in its own project that is separate from the Angular CLI project. This will lead to decoupling which in turn makes it easier to utilize a microservices architecture.
The project template creates an ASP.NET Core app and an Angular app. The ASP.NET Core app is intended to be used for data access, authorization, and other server-side concerns. The Angular app, residing in the ClientApp subdirectory, is intended to be used for all UI concerns.
The ClientApp directory contains a standard Angular CLI app. This means that the app’s capabilities are unchanged (project structure, available commands, etc.). However, there are slight differences between the Angular app created by the VS template and the one created by Angular CLI itself (via ng new). The app created by the VS template contains a Bootstrap-based layout and a basic routing example. Here is a side by side representation of the VS template and Angular CLI via ng new, respectively. From a high level they look the same. But if you were to look closer into the html of some of the components (e.g. home component) generated under the VS template application you will notice that bootstrap is being utilized.
Another difference you will notice is that the VS template is not always on the latest version of Angular. This is not a deal breaker though as starting with Angular 6 there a new feature was introduced called schematics which makes the process of updating the application much easier. Specifically, you can use the ng update schematic. It is recommended that you utilize the newly introduced Angular Update Guide to upgrade your Angular applications.
I started by selecting the versions I am upgrading between on the Angular Update Guide page:
Below are the steps that get displayed after you click on the “Show me how to update!” button:
Here is the Diff after the update:
The project is configured to start its own instance of the Angular CLI server in the background when the ASP.NET Core app starts in development mode. This is convenient because you don’t have to run a separate server manually.
There’s a drawback to this default setup. Each time you modify your C# code and your ASP.NET Core app needs to restart, the Angular CLI server restarts. Around 10 seconds is required to start back up. If you’re making frequent C# code edits and don’t want to wait for Angular CLI to restart, run the Angular CLI server externally, independently of the ASP.NET Core process. To do so:
1. In a command prompt, switch to the ClientApp subdirectory, and launch the Angular CLI development server:
2. Modify your ASP.NET Core app to use the external Angular CLI instance instead of launching one of its own. In your Startup class, replace the spa.UseAngularCliServer invocation with the following:
When you start your ASP.NET Core app, it won’t launch an Angular CLI server. The instance you started manually is used instead. This enables it to start and restart faster. It’s no longer waiting for Angular CLI to rebuild your client app each time.