简体   繁体   English

将“react js”构建到我的“asp.net core web api”的 wwwroot 文件夹时出现路由问题

[英]Routing problem when building "react js" to wwwroot folder of my "asp.net core web api"

Application information应用信息

My application uses asp.net core web API for the server-side and React js for the client-side.我的应用程序在服务器端使用 asp.net core web API,在客户端使用 React js。

Before publishing the application, the react js code is built straight to the www root folder of my asp.net Core web API.在发布应用程序之前,react js 代码直接构建到我的 asp.net Core Web API 的 www 根文件夹中。 This allows the hosting of the client and the API on the same URL.这允许在同一 URL 上托管客户端和 API。

My client uses is a SPA and makes use of react routing.我的客户使用的是 SPA 并使用反应路由。 When starting at "localhost:44362/" my asp.net core application redirects the user to the index.html file in my www-root folder.当从“localhost:44362/”开始时,我的 asp.net 核心应用程序将用户重定向到我的 www-root 文件夹中的 index.html 文件。

When this happens the client starts using the react-router package.当这种情况发生时,客户端开始使用 react-router 包。

The problem问题

let's say that our current location is "localhost:44362/dashboard" When we refresh the page we don't go directly through the react routing but first the asp.net core routing.假设我们当前的位置是“localhost:44362/dashboard” 当我们刷新页面时,我们不直接通过 react 路由,而是先通过 asp.net core 路由。 This will result in a 404 page because the actual routing can not be found by the server.这将导致 404 页面,因为服务器无法找到实际路由。

What actually should happen is that on refresh the page should go through the index.html and after execute the /dashboard request.实际应该发生的是,在刷新时页面应该通过 index.html 并在执行 /dashboard 请求之后。

What I have tried我试过的

I have tried altering the web.config file in replacing the http error status code 404 behavior to change the executionURL to /index.html as displayed below:我尝试更改 web.config 文件以替换 http 错误状态代码 404 行为,以将 executionURL 更改为 /index.html,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" />
      <error statusCode="404" responseMode="ExecuteURL" path="/index.html" />
    </httpErrors>
  </system.webServer>
</configuration>

Note: When changing the existingResponse="Replace" part to something else, this solution breaks.注意:将 existingResponse="Replace" 部分更改为其他内容时,此解决方案会中断。

This works for refreshing but breaks the regular error responses这适用于刷新但打破常规错误响应

When referring to this solution, the regular HTTP response will all be replaced.提到这个解决方案时,常规的 HTTP 响应将全部替换。

example:例子:

  • A user fills in the wrong password and submits a login request用户填写错误密码并提交登录请求
  • The server sends a 400 HTTP error response with the body "incorrect password"服务器发送带有正文“密码不正确”的 400 HTTP 错误响应
  • The error response body is replaced by the web.config错误响应正文由 web.config 替换
  • The server now only sends back "bad request" instead of "incorrect password"服务器现在只发回“错误的请求”而不是“错误的密码”

My question我的问题

Is there any way to make the routing work while still keeping the right error response messages?有什么方法可以使路由正常工作,同时仍保持正确的错误响应消息?

You could try using url rewriting in your web.config.您可以尝试在 web.config 中使用url 重写

What you want to happen is that all requests are rewritten to '/' so your react app will be loaded in, and the react routing can take over.你想要发生的是所有请求都被重写为“/”,这样你的反应应用程序将被加载,反应路由可以接管。 However, since you are running your api on the same server, you need to exclude requests which are intended for your api.但是,由于您在同一台服务器上运行您的 api,您需要排除针对您的 api 的请求。 (in this case I will assume every api endpoint will start with '/api', since you are using asp.net core) (在这种情况下,我将假设每个 api 端点都以“/api”开头,因为您使用的是 asp.net 核心)

You could add something like this to your web.config file.您可以将这样的内容添加到您的 web.config 文件中。

<rewrite>
  <rules>
    <rule name="Redirect to react">
      <match url="^(!?(api\/))(.*)$"/>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM