azure - Angular2 - 在 Azure 中托管时页面刷新 404ing

标签 azure angular angular2-routing

我正在开发 Angular2 应用程序。它使用“@Angular/common”:“2.0.0-rc.4”和“@Angular/router”:“3.0.0-beta.2”。

我的问题是,当我在某些页面上使用浏览器刷新时,我看到一条错误消息...

您要查找的资源已被删除、更名或暂时不可用。”

如果我直接点击网址,也会发生这种情况。

示例网址是... https://tilecasev2.azurewebsites.net/profile/therichmond

但是,如果您通过主页查看页面,它们可以正常工作,但只有在刷新后才可以( https://tilecasev2.azurewebsites.net )。

我的index.html 头中有以下内容...

<base href="/"> 

为什么会发生这种情况以及如何解决它?

最佳答案

HashLocationStrategy 通过在所有 Angular 路由中包含 # 来避免该问题,但并没有真正修复它。

要使没有哈希的 Angular 路由在 Azure 中像在本地开发环境中一样工作,您只需将 IIS 配置为以 root 身份重写所有请求。这让 Angular 处理路由。

为此,请将包含以下内容的 Web.config 文件添加到站点的根文件夹中:

<configuration>
<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>
</configuration>

关于azure - Angular2 - 在 Azure 中托管时页面刷新 404ing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38823595/

相关文章:

angular - 如何找到装饰有某种装饰的所有属性?

azure - 当我按照 azure 示例项目文档在 Logic App Designer 中运行我的函数时出现错误

azure - Azure Blob Store 如何处理以 .gz 结尾的文件?

azure - 我可以从 Azure 上的部署生成代码覆盖率吗?

azure - 使用参数化脚本授予对 Azure Data Lake Gen2 的访问权限

angular - 等待 firebase 加载快照

angular - ion-label 在 Ionic2 中不显示整个文本

node.js - "How to build a chatbot with anuglar and node.js"?

Angular2 Router v3,导航到不同的级别 : TypeError: Cannot read property '_routeConfig' of null

html - Angular2 Hashtag路由到Anchor不会移动页面