3

I have a React App using React Router Dom, I have been trying to figure out how to get client-side routing working, if I load the web app normally everything works fine, and I can access different components at different URLs using react-router-dom.

But if I refresh or try and access the url to a routed component directly, the server returns 404 not found as its making a request not to my SPA but the server.

I was wondering how in a .net 6.0 Web api, how to route anything not beginning with /api to my SPA.

Thanks!

1 Answer 1

2

After lots of trial and error, adding

app.MapControllerRoute(
  name: "api",
  pattern: "/api/{controller=Home}/{id?}");

app.MapFallbackToFile("/index.html");

to Program.cs, I was able to have the fallback url map back to index.html

Sign up to request clarification or add additional context in comments.

2 Comments

For some reason, I'm having this issue on Azure but not on localhost
Finally! Thank you. app.MapFallbackToFile returns routing to the client-side, so works well together with app.UseDefaultFiles(), no URL rewrite on IIS and other heavy things required. Some more details here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.