Using Angular's Base HREF in Paths
Tags: angular, javascript, typescript
Published: 2021-09-19 20:03:56

---
title: Using Angular's Base HREF in Paths
published: true
date: 2021-08-07 00:00:00 UTC
tags: angular, javascript, typescript
canonical_url: https://wildermuth.com/2021/08/08/Using-Angular-s-Base-HREF-in-Paths/
---

I was recently working with a client and they were having an odd problem with Angular. They’d build their Angular apps in isolation then move them into an [ASP.NET](http://asp.net/) Core project and their asset links would break. Let’s look at why this happens and how to address it.

The problem comes down to this simple idea. So the HTML looked like this:

```
<div>
  <img src="/assets/img/comingsoon.jpg" 
       alt="logo" 
       class="logo">
</div>

```

This works when running the project directly via ‘ng serve’ because the file is in the assets folder:

![Project](https://wilder.blob.core.windows.net/img/2021/08/08/project-tree.jpg)

<figcaption>Project Path to /assets/img/comingsoon.jpg</figcaption>

When deploying the app to an [ASP.NET](http://asp.net/) Core project, it works…as soon as it’s being deployed to the root of project. The problem is that the Angular app just renders HTML for the browser to load the images. So the absolute URL (e.g. starting with a slash) assumes that it is going to be deployed to the root of the resulting webserver. But what if it’s not deployed that way. A common case is when it’s deployed to IIS as a website in a folder of a site. For example:

![](https://wilder.blob.core.windows.net/img/2021/08/08/IISApp.png)

<figcaption>A nested app in IIS</figcaption>

In [ASP.NET](http://asp.net/)’s Razor handles this by using the tilde syntax:

```
<img src="~/img/2019/keynote-speaker.jpg" 
     alt-="Speaker Name" 
     class="img-responsive img-circle col-md-3" />

```

The tilde (~) tells [ASP.NET](http://asp.net/) Core to use the root of the app (whether that be the root of the site, or the App root in IIS). For Angular, we needed a different approach. Luckily, Angular already needed to know this for their own needs.

In Angular, they use a header value (base) to specify where this app is hosted:

```
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>NgRootdir</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

```

The **base** element’s **href** tells Angular how to deal with URLs but it doesn’t extend to HTML markup. In order to use this, we have to do some work. I found this out through a StackOverflow question answered by [Ian Campbell](https://stackoverflow.com/users/1298685/ian-campbell):

- [https://stackoverflow.com/questions/39287444/angular2-how-to-get-app-base-href-programmatically#46493276](https://stackoverflow.com/questions/39287444/angular2-how-to-get-app-base-href-programmatically#46493276)

The trick is to create a function for returning the Base HREF from PlatformLocation:

```
export function getBaseHref(platformLocation: PlatformLocation): string {
  return platformLocation.getBaseHrefFromDOM();
}

```

You can then use this to allow the value to be injected into components by creating a Provider:

```
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [
    {
      provide: APP_BASE_HREF,
      useFactory: getBaseHref,
      deps: [PlatformLocation]
    }
  ],
  bootstrap: [AppComponent]
})

```

Once this is done, you can just inject the APP\_BASE\_HREF into your components via the @Inject decorator:

```
export class AppComponent {
  title = 'ng-rootdir';
  
  constructor(@Inject(APP_BASE_HREF) public baseHref:string) {
  }
}

```

With that baseHref in your class, you can use it in the markup:

```
<div>
  <img [src]="baseHref + '/assets/img/comingsoon.jpg'" 
       alt="logo" 
       class="logo">
</div>

```

By adding the baseHref to the start of the image URL, it can work in both scenarios. With this in place you can use the Base HREF in other places as well.

Interested?

[![Creative Commons License](http://i.creativecommons.org/l/by-nc-nd/3.0/88x31.png)](http://creativecommons.org/licenses/by-nc-nd/3.0/)

 This work by [Shawn Wildermuth](http://wildermuth.com) is licensed under a [Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License](http://creativecommons.org/licenses/by-nc-nd/3.0/).  
 Based on a work at [wildermuth.com](http://wildermuth.com).

* * *

If you liked this article, see Shawn's courses on [Pluralsight](http://shawnw.me/pscourses).
 ![](http://feeds.feedburner.com/~r/shawnwildermuth/~4/i1d0ex1-7bg)