-1

I'm currently in the process of converting project frontend from .netcore web application (razor page) to reactjs, one of the road block we encounter is the dynamic url path that we applied in razor page.

As the application is deployed on IIS under default website, so the url is construct as https://localhost/Project. One of the project requirement is it can to be replicate and deploy many time, so there will be https://localhost/Project{n}. That mean the url path name need to be configurable so the frontend JavaScript will request to the correct api url. What i done was config the pathname in iis application's webconfig and inject it into the razor page, so javascript can refer to the injected pathname variable.

But when come to reactjs, it is a seperate project from backend but they share the same url, and reactjs has no way to get the configurable pathname so it can only stick to whatever set in the public_url/homepage, and it can only be set before project is build. As it is not set dynamically, the frontend point to the right default page but wrong javascript url, so it cant even run the react main chunk js.

Any lead for me to continue on? Or i need to change the way of configurable pathname?

Spdragon
  • 97
  • 1
  • 7
  • Is your problem solved? If not, I suggest you open a [support ticket](https://support.microsoft.com) for this, because it is difficult to reproduce your problem based on your description. – samwu Mar 31 '22 at 10:08
  • try this [question](https://stackoverflow.com/questions/51755370/react-router-iis-how-to-enable-routing) or this [question][2] [2]:https://stackoverflow.com/questions/10017564/url-mapping-with-c-sharp-httplistener – Shalom Prasannan Feb 18 '23 at 04:27

1 Answers1

0

In React you can use standard JS. So in order to get the current path name from your React application, you can do something like:

const pathname = location.pathname

If you need the pathname in many different places in your React project, you can set it as a context. (See this documentation)

Alternatively, if you only need it on the initial load, you can just do a useEffect in your app.js file like:

const {pathname, setPathname} = React.useState();
React.useEffect(()=>{
  setPathname(location.pathname);
}, [])
Reid
  • 4,376
  • 11
  • 43
  • 75