角度优先站点访问非根路径在本地有效,但不适用于生产 [英] Angular first site visit non-root path works locally but not in production

查看:72
本文介绍了角度优先站点访问非根路径在本地有效,但不适用于生产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 4应用程序中,如果我第一次访问该站点时在URL中键入非根路径(即 localhost:4200 / projects ),应用程序被引导,并且正确的组件呈现在浏览器中的屏幕上。

In my Angular 4 application, if I type a non-root path into the url as my first visit to the site (i.e. localhost:4200/projects), my application is bootstrapped and the correct component is rendered to the screen in the browser.

但是,一旦我通过IIS为站点提供服务,如果我转到 http://< my-domain> .com / projects ,我收到404错误(不是来自我的应用程序),并且该应用程序从未启动。

However, once I serve the site through IIS, if I go to http://<my-domain>.com/projects, I get a 404 error (not from my application) and the application is never bootstrapped.

作为首次访问生产环境中的站点,我如何访问非根路径,以识别该应用程序是Angular应用程序,并为该路径中的根目录或更高版本引导该应用程序?

How do I get a visit to a non-root path as the first visit to the site in production to recognize that the application is an Angular application and bootstrap the application for any path that is the root or beyond?

推荐答案

请参见在这里我的答案,详细了解为什么会发生这种情况。
一个简短的摘要:

See my answer here for a more detailed explanation of why this is happening. A short snippet:


在应用程序的已部署版本中,托管它的Web服务器只知道
提供它看到的一个html文件(index.html),其中
对应于您的根路径。第二个您尝试直接访问
http:// url-to-your-app / art 例如,服务器将抛出未找到的404
,因为它不认识到
可以作为资源的路径。

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 http://url-to-your-app/art 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中:

For IIS, the easiest way to fix this is to add this to your 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。

What this does is instruct IIS to respond with your index.html whenever a 404 is detected.

另一种方法是设置URL重写,但这需要安装和配置重写模块,与之相比,在我看来这太麻烦了上面的解决方案。

Another way would be to setup URL rewriting, but that requires the rewriting module to be installed and configured, and it's just too much of a hassle in my opinion compared to the solution above.

这篇关于角度优先站点访问非根路径在本地有效,但不适用于生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆