React路由不起作用,始终显示404。

huangapple go评论59阅读模式
英文:

React routing is not working and always showing 404

问题

以下是您的内容的翻译:

我的项目是React + .NET。在开发过程中,路由器运行正常,但当我发布此应用并在本地运行时,React路由器无法正常工作。有时它能工作,下一次就无法工作,显示404错误页面。

这是React路由器的代码。我已将路由器更改为react-router,但它仍然无法正常工作。

英文:

My project is React + .NET. In development, the router is working fine, but when I publish this app and run it on local, react router is not working correctly. Once working, and next time never works and shows 404 error page.

<Routes>
  <Route element={<Home isAuthorized={isAuthorized} />} path="/" />
  <Route element={<SignIn />} path="/auth/sign-in" />
  <Route element={<SignUp />} path="/auth/sign-up" />
  {isAuthorized && (
    <Route
      element={<AccountSetting isAuthorized={true} />}
      path="/auth/account-setting"
    />
  )}
  {isAuthorized && (
    <Route
      element={<Games isAuthorized={true} />}
      path="/game/games"
    />
  )}
  <Route
    element={<MainGame isAuthorized={isAuthorized} />}
    path="/game/main-game"
  />
  <Route element={<About />} path="/about" />
  <Route element={<Features />} path="/landing/features" />
  <Route element={<FreeTrial />} path="/landing/free-trial" />
</Routes>

This is the React route code. I've been changed router to react-router, but not working too.

答案1

得分: 0

I think problem is within your server-side.
我认为问题出在你的服务器端。

I'm sure you would use cors.
我确信你会使用 CORS。

services.AddCors(options => options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed((host) => true)
.AllowCredentials();
}));

app.UseCors("CorsPolicy");

Also check if your set MapFallbackToFile.
还要检查是否设置了 MapFallbackToFile。

app.MapFallbackToFile("index.html");

英文:

I think problem is within your server-side.
I'm sure you would use cors.

services.AddCors(options => options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowed((host) => true)
.AllowCredentials();
}));
app.UseCors("CorsPolicy");

Also check if your set MapFallbackToFile.

app.MapFallbackToFile("index.html");

huangapple
  • 本文由 发表于 2023年5月10日 15:21:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定