0

I have follow this example to add web.config file to react project.

https://javascript.works-hub.com/learn/how-to-host-a-react-app-on-azure-10042

I have added below code to my web.config.

 <appSettings>
    <add key="SERVICE_BASE_URL"value="https://localhost:44385/"/> 
</appSettings>

I need to read this value from react. Is there a way to do this. please give any suggestions. I tried this way.

import "../web.config";
let SERVICE_BASE_URL = '<%= System.Configuration.ConfigurationManager.AppSettings["SERVICE_BASE_URL"].ToString() %>';
AlexDemo
  • 59
  • 6
  • Does this answer your question? [How can I read key value from web.config in javascript file?](https://stackoverflow.com/questions/31091539/how-can-i-read-key-value-from-web-config-in-javascript-file) – DeepDave-MT Apr 08 '22 at 05:17
  • 1
    @DeepDave-MT I will try with this. Thank you very much! – AlexDemo Apr 08 '22 at 05:21
  • I tried like thi value is not reading let SERVICE_BASE_URL = ''; – AlexDemo Apr 08 '22 at 06:02

1 Answers1

0
  • Create a .env file at the root of your React project to declare environment variables.
  • Environment variables in React must be prefixed with REACT APP_, or else the variable would be ignored during bundling.
  • You can access these variables without requiring any import statements by using the process.env global object.
  • You can also change the settings of your environment variables depending on the current situation. The variables can be stored in .env.development for a development environment.
  • Similarly, you can maintain the variables in .env.production for a production setting.

Please refer Store and Read Configuration Files Using React , Environment Variables in ReactJS. and SO thread for more information.