Pass ASP.NET Core Appsettings Values to Angular

As part of getting my set of Identity Server 4 sample applications to run in Azure, I needed a way in the Client Application to pass some configuration values from  appsettings.json to the Angular front end that could be used both during server-side rendering and client-side rendering. This application is using JavaScriptServices. This solution may need tweaking if your application isn’t using JavaScriptServices. The code for the client application can be found here.


In this example, we need to pass the address of our Identity Server and API from  appsettings.json to Angular. The following is the settings file for this example.

Providing Configuration Data to Angular

In this application, Angular is loaded from the index action of the home controller. This view can be found in the  Views/Home folder in the  Index.cshtml file. The following is the file before any changes.

The first change needed is to inject the configuration data using ASP.NET Core’s DI system. Add the following two lines at the top of the file.

Now the configuration data from the application is available to this view. Next, we need to pull a couple of values out of the configuration data and pass it to the Angular application. To do this we are going to use the  asp-prerender-data tag helper. You can read more about it in the official docs. The idea is you construct an object which is then serialized and stored in In our example, we are passing the URLs for the Identity and API Applications.

The above is creating a new object with an  apiUrl property and an  identityUrl property. The following is the full completed view for reference.

Angular Server-Side Boot

When Angular gets prerendered on the server-side it runs the code in the  boot.server.ts file. This is where we will set up the providers needed on for the server side prerender. This is the bit that I missed for the longest time when trying to get this example going. I kept trying to find a way to add the providers in the  app.module.server.ts file. Add any providers you need to the  providers constant. For example, the following is passing URLs for an API and Identity Server in addition to the defaults provided by JavaScriptServices.

Lower in the same file we can pass through the configuration values to the client side render as globals on the window object. To do this add a  globals property to the object being passed to the  resolve call.

The above will have the URLs as part of a single object, but you could have each URL as its own property if you prefer.

Angular Client-Side

Now that the server-side has providers for API URL and Identity URL we need to provide the client-side with the same capabilities. These changes will be in the  app.module.browser.ts file. The first step is to add providers for each.

Next, we need functions to return the URLs from the  url_Config property of the window object which the following two functions do.

Wrapping Up

With the above, you can now use your configuration values from ASP.NET Core and pass them through to your Angular application. In hindsight, the process is pretty simple, but getting to that point took me much longer to figure out than I would like to admit. I hope this post saves you some time!

Also published on Medium.

13 thoughts on “Pass ASP.NET Core Appsettings Values to Angular”

    1. Glad it was helpful! I do have a working example that can be found here which is part of my Identity Server 4 example repo. The link leads to the client application, but the repo also contains an Identity Server project and an API project.

  1. Oh my god, thank you!
    I have been trying to solve this problem for at least a whole day without luck… this should be included in the official docs i think, there is not much info on how to pass settings from serverside to angular apps..

    You saved my upcomming months man 🙂

  2. Hello!

    Before all, thank you very much! i have a problem with your solution. I did all the tutoral but I have the following error:

    TypeError: Cannot read property ‘clientID’ of undefined

    And this is my code:

    export function clientIDFactory() {
    return (window as any).url_Config.clientID;

    I dont know why (windows).url_Config is not define. I did it:

    setImmediate(() => {
    html: state.renderToString(),
    globals: { url_Config: }

    I waiting for you response!

    Thank you 🙂

    1. It sounds like you are using trying to use the window object as part of a server-side render. Window is only valid on the client side.

      1. Yes, you are right! But I put this code

        export function clientIDFactory() {
        return (window as any).url_Config.clientID;

        in app.browser.module.ts! That file is for client side not?

        I saw your project example and my project have the same code! I dont know what is happening 🙁

        1. Yes, that file should be on the client side. Have you cloned my repo to see if you can get it to work? If it does work then it will be a matter of comparing the two until you find the differences between the two.

  3. Thank you for your time and effort, Eric.
    This tutorial was very helpful for our ASP.NET Core 2.0 Azure project.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.