简体   繁体   English

如何在Microsoft IIS上使用react router(createBrowserHistory)来使路由工作?

[英]How to use react router (createBrowserHistory) on Microsoft IIS to make routing working?

I am using react-router (createBrowserHistory) for my react app. 我正在使用react-router(createBrowserHistory)作为我的反应应用程序。

Below is my code of 以下是我的代码

var ReactDOM = require('react-dom') ;

var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var Link = ReactRouter.Link;
var browserHistory = require('react-router');
var createBrowserHistory = require('history/lib/createBrowserHistory');

var CL = require('./page1/1.jsx');
var Validation = require('./page3/3.jsx');
var Infos = require('./page4/4.jsx');
var Confirm = require('./page7/7.jsx');
var Upload = require('./page8/8.jsx');

module.exports = (

  <Router history={new createBrowserHistory()}> 
    <Route path='/' component={CL}></Route>                                                                                                                                     
    <Route path='/validation' component={Validation}></Route>                                                                                                                                     
    <Route path='/infos' component={Infos}></Route>                                                                                                                                     
    <Route path='/confirm' component={Confirm}></Route>                                                                                                                                     
    <Route path='/upload' component={Upload}></Route>                                                                                                                                     
  </Router>
)

Wen run IIS on local, I go to localhost on browser, I can get "CL" component and show on page, however, if I go to /validation, I will get Wen在本地运行IIS,我在浏览器上访问localhost,我可以获得“CL”组件并在页面上显示,但是,如果我去/验证,我会得到

Failed to load resource: the server respond with status of 404 (Not Found)

Anyone know what need to add to IIS or my js code to make this routing work? 任何人都知道需要添加到IIS或我的js代码以使此路由工作?

I think you are talking about react-router or similar stuff below configuration on iis 7 works for me 我认为你在谈论反应路由器或类似的东西,配置在iis 7上适合我

<rules>
     <rule name="Rewrite Text Requests" stopProcessing="true">
         <match url=".*" />
             <conditions>
                        <add input="{HTTP_METHOD}" pattern="^GET$" />
                        <add input="{HTTP_ACCEPT}" pattern="^text/html" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             </conditions>
             <action type="Rewrite" url="/index.html" />
     </rule>
</rules>

I've struggled with this a little bit so I write it for the next person: First update the web.config file (as Bo Chen mentioned). 我有点挣扎于此,所以我为下一个人写了:首先更新web.config文件(如Bo Chen所提到的)。

 <system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite Text Requests" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_METHOD}" pattern="^GET$" />
            <add input="{HTTP_ACCEPT}" pattern="^text/html" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>

This will find every request and rewrite it to /index.html instead. 这将找到每个请求并将其重写为/index.html

So you need to have a index.html in the root of your project. 所以你需要在项目的根目录中有一个index.html。 Also pay attention to the extension of the file. 另外要注意文件的扩展名

One additional thing to note: make sure you have the URL Rewrite extension installed. 还有一点需要注意:确保安装了URL Rewrite扩展。

https://www.iis.net/downloads/microsoft/url-rewrite https://www.iis.net/downloads/microsoft/url-rewrite

You may also need to do an iisreset after installation. 安装后您可能还需要执行iisreset

There are two things you can do... 你可以做两件事......

  1. Add virtual paths to point to your spa folder for every route. 添加虚拟路径以指向每个路径的spa文件夹。
  2. Use IIS URL rewrite module. 使用IIS URL重写模块。 This is a long discussion to your using the SO app, so here's a post describing it- http://weblogs.asp.net/owscott/rewrite-vs-redirect-what-s-the-difference 这是您使用SO应用程序的长时间讨论,所以这里有一篇描述它的帖子 - http://weblogs.asp.net/owscott/rewrite-vs-redirect-what-s-the-difference

Another option is using an application on top of IIS to ensure you've got other features that might come in handy, like the ASP.NET framework with MVC on top of it. 另一种选择是在IIS之上使用应用程序,以确保您拥有可能派上用场的其他功能,例如基于MVC的ASP.NET框架。 You could have a route there that simply catches all requests that aren't specifically mapped (like /api, /content) and route these to the html in such a way that your React app can handle it. 你可以在那里找到一条路线,它只是捕获所有未经专门映射的请求(例如/ api,/ content),并将这些请求路由到html,以便你的React应用程序可以处理它。 The benefits over pure IIS really depend on your circumstance. 纯IIS的好处实际上取决于您的情况。

The following is my route configuration for ASP.NET Core, to give you an example: 以下是我对ASP.NET Core的路由配置,给你一个例子:

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action}/{id?}",
                defaults: new {controller = "Home", action = "Index"},
                constraints: new { controller = new NotEqualConstraint("api")});

            routes.MapRoute("api", "api/{controller}/{action}/{id?}");
            routes.MapRoute("React failover", "app/{*uri}", new {controller = "App", action = "Index"},
                new {controller = new NotEqualConstraint("api")});

        });

Here is a working web.config file 这是一个有效的web.config文件

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
     <rule name="Rewrite Text Requests" stopProcessing="true">
         <match url=".*" />
             <conditions>
                        <add input="{HTTP_METHOD}" pattern="^GET$" />
                        <add input="{HTTP_ACCEPT}" pattern="^text/html" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
             </conditions>
             <action type="Rewrite" url="/index.html" />
     </rule>
</rules>
    </rewrite>
  </system.webServer>
</configuration>

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

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