【问题标题】:Url rewriting breaks web api callsUrl 重写中断了 Web api 调用
【发布时间】:2015-08-18 22:11:26
【问题描述】:

在我的 angularjs 应用程序中,我使用 UI.Router 并尝试添加 html 5 模式以从 URL 中删除 #,我从 github 复制 ui 路由器的设置,有一个部分 Azure IIS Rewrites settings,这是设置

<system.webServer>
  <rewrite>
    <rules> 
      <rule name="Main Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

即使页面刷新也一切正常,但它会中断我的 Web API 调用。

当我在 web api 上调用任何 get 方法时,它总是返回 index.html 内容。如果尝试调用任何 post 方法,它会给出错误:

HTTP 错误 405.0 - 方法不允许

您正在查找的页面无法显示,因为正在使用无效的方法(HTTP 动词)。

任何帮助解决它。

【问题讨论】:

    标签: angularjs url-rewriting angular-ui-router asp.net-web-api


    【解决方案1】:

    我通过再添加一个模式来跳过来解决它

    <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
    

    现在可以正常使用了

    【讨论】:

    • 嘿,你是怎么做到的?您是否在 IIS 中的同一应用程序池和项目文件夹结构中同时托管您的 Web 前端应用程序和 webapi?
    【解决方案2】:

    了解 IIS 重写的工作原理。我相信条件部分可以增加额外的条件,这些条件可以忽略一些 url 模式,例如其中包含 /api 的模式。 如果您查看此博客中的最​​后一个示例http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/#asp-net 有一个条件可以跳过.axd,API url 也可以做类似的事情。

    &lt;add input="{URL}" negate="true" pattern="/API/" /&gt;

    虽然我不确定上述模式是否有效。您需要对其进行试验,并可能在此处发布要使用的正确模式。

    【讨论】:

      猜你喜欢
      • 2019-07-12
      • 2019-01-17
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多