【问题标题】:IIS Rules with React Router v4带有 React Router v4 的 IIS 规则
【发布时间】:2018-10-03 01:22:47
【问题描述】:

我正在尝试使用 BrowserRouter 让我的路由与 IIS 和 React Router v4 一起使用。

在我的本地主机上,我的所有路由都按预期工作。 React Router 处理我想要的一切:

现在,在 IIS 中,我设置了一个忽略“/appfolder/api/*”的规则,以便为我的应用程序提供 api。这行得通。

如何让 IIS 重定向到“http://www.example.com/appfolder/dest/CODE”并让 React Router 根据其规则处理它。如果我重定向到“/appfolder/index.html”,我会丢失我想保留给 ReactRouter 处理的“代码”。

如果我尝试使用正则表达式捕获组重定向到“http://www.example.com/appfolder/dest/CODE”,我会得到一个“ERR_TOO_MANY_REDIRECTS”。

我目前在我的 web.config 中有这条规则:

    <rule name="ReactRouter Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/appfolder/api/" negate="true" />
      </conditions>
      <action type="Redirect" url="/appfolder/dest/CODE" />
      <!-- also tried this -->
      <!-- action type="Redirect" url="/appfolder/index.html" /-->
    </rule>

【问题讨论】:

    标签: reactjs iis url-rewriting react-router


    【解决方案1】:

    我找到了解决方案。我将操作类型更改为 Rewrite,它就像一个魅力。

    IIS规则最终解决方案:

    <rule name="ReactRouter Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/appfolder/api/" negate="true" />
      </conditions>
      <action type="Rewrite" url="/appfolder/index.html" />
    </rule>
    

    【讨论】:

      【解决方案2】:

      我能够使用 react-router v4 让事情为我工作的唯一方法是使用 HashRouter:https://reacttraining.com/react-router/web/api/HashRouter

      无需更改 web.config!

      【讨论】:

      • HasRouting 非常棒,直到您需要使用 OAuth,其中回调 URL 被禁止具有哈希值..
      • @Jack7 此声明的来源?
      猜你喜欢
      • 2018-03-31
      • 2018-11-30
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      相关资源
      最近更新 更多