0

I have a project created using the VS 2022 ASP.NET Core with Angular template.

Everything about the project itself is good, but I am trying to figure out how to get the Angular app to load the .NET Core's appsettings.json to use for config, so it's one shared file for ease of use/deployment.

The file structure of the deployed project looks like

  • Main Deployment Folder
    • api.exe (and all other framework files)
    • appsettings.json
    • wwwroot folder
      • assets folder
      • index.html (and all other Angular files)

Initially I went down the path of creating an Angular service to load config from a file as in this answer https://stackoverflow.com/a/54793384/12899737

However, I then realized that I couldn't point to the appsettings.json in the deployment folder above the wwwroot as that is not being served in the same way and so it was unavailable.

I could use the angular.json build settings to include the appsettings, but that just creates a copy of it at build time and puts you back to essentially having to manage 2 config files.

I am also aware that some examples serve the config settings via an API call, I'm considering that as a last resort option but would prefer to keep it file based if possible.

The main 2 questions I have are:

  1. Is there any way to have a service in the Angular app served from the wwwroot folder that can access the appsettings.json file directly in the parent folder, without that file being served?

  2. Is it crazy to consider moving the appsettings.json down a folder into the wwwroot so the Angular app has access to it, and then editing the WebApplication.CreateBuilder(args) in the API to include that location for appsettings.json?

Thank you for your input!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 6
    As an aside... I'd caution against making the server-side configuration data openly available to client-side code. There are likely to be values in there that users *shouldn't* see. (System account passwords, connection strings, external API keys, etc.) – David Jul 24 '23 at 20:38

1 Answers1

0

One possible solution is to use the ConfigurationBuilder class in both your ASP.NET Core and Angular projects to read the appsettings.json file from a shared folder. You can find an example of how to do this in this Stack Overflow answer.

Another possible solution is to create a custom service in your Angular project that makes an HTTP request to your ASP.NET Core project to get the configuration settings from the appsettings.json file.

Saeed Gholamzadeh
  • 932
  • 1
  • 6
  • 22
  • We decided to not get too complicated and just go with the HTTP request method, as none of the settings we're transferring are sensitive – Logan McManus Jul 25 '23 at 14:08