Get Started with Angular on .NET Core 2.1 (Part One)
Tags: dotnet, angular, typescript, azure
Published: 2018-09-06 13:43:33

---
title: Get Started with Angular on .NET Core 2.1 (Part One)
published: true
tags: dotnet,angular,typescript,azure
canonical_url: https://blog.jeremylikness.com/get-started-with-angular-on-net-core-2-1-part-one-2effcfe8fae9
---

### Get Started with Angular on .NET Core 2.1 (Part One)

#### Cross-platform client-side JavaScript SPA framework, meet cross-platform server-side .NET Core framework.

[Angular](https://angular.io/) is one of the top “go to” frameworks for developing client-side Single Page Applications (SPA). It supports the “[3 D’s of Modern Web Development](https://blog.jeremylikness.com/the-three-ds-of-modern-web-development-55d69fe048da)” out of the box with built-in declarative templates that support data-binding and a highly configurable dependency injection (DI) framework. I’ve worked with Angular since it was “[AngularJS](https://angularjs.org/).” I have no ambition to compare it with other frameworks (like [ReactJs](https://reactjs.org/)or [Vuejs](https://vuejs.org/)) in this post; the goal is to illustrate how Angular works with [.NET Core](https://jlik.me/d90).

![](https://cdn-images-1.medium.com/max/800/1*54IUNsFy3eZS3Fj1K4LkKQ.png)

.NET Core is a cross-platform and open source implementation of the APIs defined by the [.NET Standard](https://jlik.me/d91). Apps written for .NET Core are capable of running on Windows machines (as far back as Windows 7), Mac OS and several flavors of Linux. Apps are authored in several languages; [C#](https://jlik.me/d92) and [F#](https://jlik.me/d93) are the most popular. Write everything from back-end APIs to dynamic websites, and as a bonus there is a [template to integrate Angular with .NET Core](https://jlik.me/d94). There are many options to deploy and host the apps in the Azure cloud.

Recently, I presented a session about Angular and .NET Core. This series is based on the content and demos of the session. Click or tap to download the presentation: [Angular in the .NET World](https://jlik.me/d95). The code is all contained in the [Angular and .NET GitHub repo](https://github.com/JeremyLikness/angular-net).

You can watch the full presentation here:

{% youtube xrzpYMstTvc %}

This is a four-part series. The full series will be posted over time:

1. **Get Started with Angular on .NET Core 2.1 (you are here)**
2. The Angular .NET Core 2.1 Template
3. Server-side Rendering (SSR) with Angular in .NET Core 2.1
4. Deploy Angular and .NET Core 2.1 to the Azure Cloud

In this post I’ll share three specific ways Angular and .NET Core can work together, along with a few pointers for hosting the finished apps. I assume you have some familiarity with both frameworks. If not, follow the links earlier in this article because the documentation for both technologies is very thorough with easy-to-follow tutorials.

#### Approach One: “Gotta Keep ’em Separated”

A common approach to building SPA applications is to package the website as a set of static assets and host as static web pages, then stand up a separate set of back-end services that the application communicates with. .NET Core supports [building REST APIs](https://jlik.me/d96) out of the box, with integrated [support for various authentication and authorization schemes](https://jlik.me/d97) as well as [Swagger](https://jlik.me/d98)to document the end points.

In this approach, you take the [standard steps to stand up your Angular app](https://angular.io/guide/quickstart) as a separate project. For the [sample app](https://github.com/JeremyLikness/angular-net) we’ll make a few tweaks, but first I want to walk through the .NET Core back-end. After you [install .NET Core](https://jlik.me/d99), create a directory and run the command to create a new Web API project:

`dotnet new webapi`

Rename the `ValuesController` to `BifurcController` and populate it with the following code:

{% gist https://gist.github.com/JeremyLikness/4cc08e3fb7788eb0b570ee67e0394180 %}

The `HttpGet` attribute defines the verbs this endpoint can take and includes a route designation that expects a variable called r. This will automatically get parsed and passed to the method. The method also takes query parameters (flagged by `FromQuery`) that indicate how many iterations to run (`iterations`) and how many results to ignore (`skip`). It then recursively runs:

`r(x) = x \*n\*(1.0-x)`

The results are stored in an array and returned. At this point you can launch the app with:

`dotnet run`

Then navigate to a properly constructed URL to see the results:

![](https://cdn-images-1.medium.com/max/800/1*lv0sxWCL-7L1SdXE4C8rxg.png)<figcaption>A sample API run</figcaption>

On the Angular side, this is a simple service that fetches results based on a passed in `r` value:

{% gist https://gist.github.com/JeremyLikness/fabc8a9efd9fdf2e667c81bb7e97a77a %}

Add a `canvas` declaration to the main `app.component.html` page:

`<canvas id=”mainCanvas” #mainCanvas></canvas>`

Then add the code to call the service and render the results:

{% gist https://gist.github.com/JeremyLikness/d87e31ac886ba5cf89c71fa3848fb51c %}

This is when I add a simple disclaimer:

> I’m cutting corners to keep the demo simple. Ordinarily you’d inject the base URL from a service and build the graph as it’s own component. We’re keeping it simple here!

If all goes well you’ll see something like this:

![](https://cdn-images-1.medium.com/max/1024/1*F9ntPC5gUV8OmR9y_6931Q.png)<figcaption>Static app with .NET Core backend</figcaption>

… and that’s how we use fractals to teach code!

Although this approach works perfectly fine, there are a few drawbacks. You have to manage two separate projects and debug them separately as well. In the next article, you will learn how to leverage a built-in .NET Core template to manage the client and server components of your web app in a single project.

Regards,

![](https://cdn-images-1.medium.com/max/197/1*KwXn3ElS7vnWIk8lNVhBsA.gif)

Next: The Angular .NET Core 2.1 Template


* * *