1

I'm building an application with Angular with a .NET core backend. I have set an environment variable in both Angular and .NET.

And I'm wondering, is there a way to get the .NET's "ASPNETCORE_ENVIRONMENT" variable from Angular?

1 Answer 1

1

Expose an API that returns the current environment name. You can then use it from your Angular App.


Update:

  1. Define your environment variable in appsettings.json (preferably in a json config file of it's own).
  2. Set the ASP.NET CORE app to use it. Steps from the docs.
  3. You can now use the contents of that file as constants in your app using a method described here. You may of course replace grunt with gulp (gulp-ng-constant) if you'd prefer. (I haven't tried this step, but I'd be happy to help).

Build events are supported. So if you use environment variables you can generate a config file on the fly (via build events) and your angular app can then access that as mentioned in step 3 above.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I was hoping not to do this and reading the environment from a config on the machine.
@MikeBovenlander This was possible in the classic asp.net web api apps via build tasks. Not quite sure if the new csproj format supports it.
@MikeBovenlander I've updated the answer! Hope it helps. If you manage to find a solution better than mine, don't forget to answer your question, because I'm quite interested in this myself.
Thanks for the update, I'm not there yet but I think I can use your update as a handle. Ill dive back in this weekend and will keep you posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.