Skip to content
Advertisement

Building a SPA with react on top of MVC Routes

I have an API that has routes managed by MVC.
On top of that i want to build a SPA with react.
However the routes I build from inside my react app cannot be reached, i get an 404 from ISS, here us a stub from my code.

JavaScript

When I execute this code as a standalone whithout the backend, it works flawlessly.
Is there a way to tell MVC to render reacts routes for a set url, let’s say “/app/*”.

Thanks in advance.

Advertisement

Answer

As i mentioned in my comment, i may have a solution that can fit your needs.
This solution requires an MVC Controller and a relevant MapRoute().
The general idea is to deliver the index.html page via MVC using a controller and cshtml and configure on react-router a basename when creating browserHistory.
The key here is that the basename value must contain the entire path (without the domain) up to the controller name.
for example:
given this controller:

JavaScript

and cshtml:

JavaScript

and routeMap:

JavaScript

Now, lets say your app is published under http://example.org/appName/ then you’ll need to make sure react-router won’t delete the "appName" portion of the URL when changing it.
for example, if you’re in the Home Pagehttp://example.org/appName/home
and you move to the About Page, you want react-router to keep the "appName"
like so: http://example.org/appName/react/about
and not like so http://example.org/about.

Although this will work just fine as you’re not posting this URL back to the server, you will face a problem when you will try to go directly to the “About” page. the server will return a 404 when you send the request with this URL: http://example.org/about/
instead of this one: http://example.org/appName/react/about.

My soulution to this problem is to pass react-router a basename that includes the appName + sub folders (if any) + the controller name.

I’m using useRouterHistory and creating the browserHistory instead of import it from react-router

JavaScript

the appName variable is as follow:

JavaScript

This can be refactored as a function but basically it gets the pathname up to the controller name (as a convention with your MVC app) including the controller name.
that way react-router will keep this path on every URL change. In our case "appName/react".

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement