【问题标题】:Host frontend (react) and backend (ASP.NET Web Api) under the same Website in IISIIS 中同一网站下的主机前端(react)和后端(ASP.NET Web Api)
【发布时间】:2021-05-12 02:11:00
【问题描述】:

我有 2 个应用程序。

我希望前端(反应)在 url 上可用: localhost:5001/ekadry

和后端(c# 中的 web api)在 url 上可用: localhost:5001/ekadryapi

这是它在 IIS 中的样子:

当我访问这些网址时,每个应用程序似乎都在工作。当前端向后端请求某些内容时会出现问题 - 然后我收到错误 405 Method Not Allowed:

当后端和前端作为不同的网站托管在不同的端口上时,不会出现此问题。 谁能给我解释一下?

【问题讨论】:

  • 看看这个如果有帮助 - stackoverflow.com/questions/59485621/…
  • 尝试从 iis 中删除 WebDAV 功能。通过取消选中该功能。image 进行更改后重新启动 iis。
  • 这不是 WebDAV 问题,因为我没有它

标签: c# reactjs iis asp.net-web-api


【解决方案1】:

我认为您必须在 API web.config 中授予对客户端的访问权限

类似下面的标签:

<configuration>
<system.webServer>
   <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Content-Type, Token, Accept, 
            Accept-Language, Content-Language,X-ATJ-Auth-Token,X-ATJ-Username" />
            <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
        </customHeaders>
   </httpProtocol>
    <handlers>
      .
      .
      .
    </handlers>
  </system.webServer>
</configuration>

【讨论】:

  • 感谢您的回答。我在 web api 中将这些行添加到 web.config 中,但我看不出有什么不同,仍然是同样的错误:(
【解决方案2】:

我在 web.config 中添加了这条规则,现在前端可以访问后端。问题解决了。

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

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 1970-01-01
    • 2021-09-22
    • 2012-08-24
    • 2013-01-12
    • 2019-02-26
    • 2015-11-24
    • 2020-12-05
    • 1970-01-01
    相关资源
    最近更新 更多