Angular 2 and .NET Core – route directly to your components

(I’ve put together the source code for the entire Angular 2 and .NET Core weather app, click the button to get hold of it.)

Get the code

Last time out we took a quick look at creating a simple HelloWorld component and displayed it on one of our existing web pages using its selector.


Which rendered something like this…

Using the helloworld selector you can now include this component anywhere you like in your app, it doesn’t matter how many times you use it. Anywhere you can write html, you can include your component; this includes in the template of another component. For example, you can display your helloworld component in the template for the home component.

<h1>Hello, world!</h1>
<p>Welcome to your new single-page application, built with:</p>
    <li><a href=''>ASP.NET Core</a> and <a href=''>C#</a> for cross-platform server-side code</li>
    <li><a href=''>Angular 2</a> and <a href=''>TypeScript</a> for client-side code</li>
    <li><a href=''>Webpack</a> for building and bundling client-side resources</li>
    <li><a href=''>Bootstrap</a> for layout and styling</li>

Route to a component

All well and good, but what if we want to show this content by itself, not nested in another page.

The Angular 2 router is a powerful tool which enables you to do just that. Specifically you can configure it to route to a component when the user enters a specific url for your site.

So in this case, we could configure it so that navigating to routes directly to the hello world component.

Open up your app.module.ts file and make the following simple change in routing section.

    imports: [
        UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'counter', component: CounterComponent },
            { path: 'fetch-data', component: FetchDataComponent },
            { path: 'hello', component: HelloWorldComponent },
            { path: '**', redirectTo: 'home' }

Incidentally, the last route (**) means that any unrecognised url (e.g. /nothingmuchimparticular) will redirect to the home page.

Now test it out by running your site and changing the url to end with /hello.

You might be wondering how angular decides where to render the component’s html? It simply looks for the router-outlet selector and renders your component there.

In our case, the router-outlet tag is included in the main app component (app.component.html).

<div class='container-fluid'>
    <div class='row'>
        <div class='col-sm-3'>
        <div class='col-sm-9 body-content'>

Link to your route

If you want to display a link to your new route you’ll need to modify your site’s navigation. As with everything else, the nav menu is its own component.

Modify navmenu.component.html as follows.

                <li [routerLinkActive]="['link-active']">
                    <a [routerLink]="['/fetch-data']">
                        <span class='glyphicon glyphicon-th-list'></span> Fetch data
                <li [routerLinkActive]="['link-active']">
                    <a [routerLink]="['/hello']">
                        <span class='glyphicon glyphicon-user'></span> Hello

Now your users can navigate to your friendly greeting…

Rain rain, go away…

Enough HelloWorld, next time we’ll get on with building a more useful component.

We’re going to create a simple component that lets you enter a city name and then see the current weather for that city.

To save storing this data ourselves we’ll use a third party service and connect to it via Web API. That means we’ll go from Angular 2 (our SPA) to .NET Core Web API, which will in turn access the third party weather service.

photo credit: Bill Herndon Farm Road 2775 via photopin (license)