How to Connect to Project Running on Localhost from Simulator?

24.07.2022 13:40
#REACT #NET-CORE #APİ #CORS #SİMULATOR

How to Connect to Project Running on Localhost from Simulator?

I have a .Net Core API running on localhost and a React project running on localhost. I need to test this react project on simulator. I started getting an access error when I tried to connect to localhost:3000 from the Simulator. In this case, the following questions arise:

  1. Why doesn't a project running on localhost work in a simulator on the same computer?
  2. How to connect to a project running on localhost from the simulator?

The answer to the first question is, as far as I can see after trying, we should directly use the local ip address of the device instead of localhost.

Coming to the answer to the second and main question, although it varies according to the project type and project configuration, the general solution should be to change the ip routings and allowed ip addresses in the project. So what are they?

  • In a .Net Core project, you must also add your local ip address to the applicationUrl of LaunchSettings.json in the Properties folder. Otherwise, it will only be accessible from the links specified there.Localhostta çalışan projenin simülatör üzerinde de çalışması için gereken ip yapılandırmasının launchSettings.json üzerinde ayarlanması.
  • If the .Net Core project you are using is an API project, if there are CorsLinks or AllowedOrigins that you specify in appsettings.json, you should add your local ip address to the links in them. Otherwise, you may get a cors error when you want to access it from your UI project.Localhostta çalışan projenin simülatörde de çalışması için gereken ip yapılandırmasının appsettings.json üzerinde yapılması.
  • On the UI side, if you have an existing access like https://localhost:5001/... to access your APIs, you need to change it to your local ip address. Otherwise you may not be able to access your API and get a cors error.