Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Shawn Wildermuth
Shawn Wildermuth

Posted on • Originally published atwildermuth.com on

     

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 anASP.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"       ></div>
Enter fullscreen modeExit fullscreen mode

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

Project

Project Path to /assets/img/comingsoon.jpg

When deploying the app to anASP.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:

A nested app in IIS

InASP.NET’s Razor handles this by using the tilde syntax:

<img src="~/img/2019/keynote-speaker.jpg"      alt-="Speaker Name"      />
Enter fullscreen modeExit fullscreen mode

The tilde (~) tellsASP.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>
Enter fullscreen modeExit fullscreen mode

Thebase element’shref 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 byIan Campbell:

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

export function getBaseHref(platformLocation: PlatformLocation): string {  return platformLocation.getBaseHrefFromDOM();}
Enter fullscreen modeExit fullscreen mode

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]})
Enter fullscreen modeExit fullscreen mode

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) {  }}
Enter fullscreen modeExit fullscreen mode

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

<div>  <img [src]="baseHref + '/assets/img/comingsoon.jpg'"        alt="logo"       ></div>
Enter fullscreen modeExit fullscreen mode

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

This work byShawn Wildermuth is licensed under aCreative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Based on a work atwildermuth.com.


If you liked this article, see Shawn's courses onPluralsight.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Shawn Wildermuth has been tinkering with computers and software since he got a Vic-20 back in the early ‘80s. He has been a Microsoft MVP, Pluralsight Author, and filmmaker.
  • Location
    Atlanta, GA
  • Education
    Some College
  • Pronouns
    he/him
  • Work
    Mind at Wilder Minds LLC
  • Joined

More fromShawn Wildermuth

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp