加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular第一次站点访问非根路径在本地工作但不在生产中

发布时间:2020-12-17 07:35:33 所属栏目:安全 来源:网络整理
导读:在我的Angular 4应用程序中,如果我在第一次访问该站点时键入了一个非根路径(即localhost:4200 / projects),我的应用程序将被引导并且正确的组件将呈现在浏览器的屏幕上. 但是,一旦我通过IIS提供网站,如果我转到http:// my-domain .com / projects,我会收到
在我的Angular 4应用程序中,如果我在第一次访问该站点时键入了一个非根路径(即localhost:4200 / projects),我的应用程序将被引导并且正确的组件将呈现在浏览器的屏幕上.

但是,一旦我通过IIS提供网站,如果我转到http://< my-domain> .com / projects,我会收到404错误(不是来自我的应用程序),并且应用程序永远不会被引导.

作为第一次访问生产中的站点,我如何访问非根路径,以识别该应用程序是一个Angular应用程序并为任何根路径或更远的路径引导应用程序?

有关发生这种情况的更详细说明,请参见 my answer here.
一个简短的片段:

On the deployed version of your app,the web server hosting it knows
only how to serve the one html file it sees (index.html),which
corresponds to your root path. The second you try to access directly
07001 for example,the server will throw a 404
not found,as it does not recognize that as the path of a resource it
can serve.

对于IIS,解决此问题的最简单方法是将其添加到web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors>   
            <remove statusCode="404" subStatusCode="-1" />                
            <error statusCode="404" path="/index.html" responseMode="ExecuteURL" />                
        </httpErrors>
    </system.webServer>
</configuration>

这样做是指示IIS在检测到404时使用index.html进行响应.

另一种方法是设置URL重写,但这需要安装和配置重写模块,与上述解决方案相比,我认为这太麻烦了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读