I was able to pull this off by jumping through these hoops; for this example, I'm going to get the web.config "environment" app key value:
- In your react View's .cshtml file, put this in the
<head>:
<script>
var REACT_APP_RUNTIME_ENVIRONMENT = window.REACT_APP_RUNTIME_ENVIRONMENT = '@System.Web.Configuration.WebConfigurationManager.AppSettings["environment"]';
if (REACT_APP_RUNTIME_ENVIRONMENT.indexOf('Configuration') != -1){
REACT_APP_RUNTIME_ENVIRONMENT = window.REACT_APP_RUNTIME_ENVIRONMENT = 'dev'; // Backup value in case the above fails
}
</script>
In the example above, I added a conditional as a fallback so you can use a flat index.html page instead of a ASP.NET MVC project and modify the runtime environment variable there instead.
- In your react project, you can now access
window.REACT_APP_RUNTIME_ENVIRONMENT and it will pull the value "environment" from the web.config:
<appSettings>
<add key="environment" value="test" />
</appSettings>
It's important to note that I've only tested this in a ASP.NET MVC project.