在IIS上托管Angular2应用后,直接URL无法正常工作 [英] After hosting Angular2 app on IIS direct url is not working

查看:89
本文介绍了在IIS上托管Angular2应用后,直接URL无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经开发了Angular2 App.

We have developed Angular2 App.

当我们在带有"ng serve"的cli-cli下运行时,它运行良好.

It is working fine while we are running under angular-cli with 'ng serve'.

将应用程序托管到IIS 7.5之后,我们可以浏览到应用程序的根目录而不会出现很多问题,并且可以从创建的应用程序导航中导航到其他任何视图.

Once we host the app to IIS 7.5, we can browse to root of the app without much of the problem and we can navigate to whatever other views from app navigation created.

但是当用户尝试加载针对特定路线或组件的网址时,问题就开始了.

But issue starts when user try to load url which is targeting specific route or component.

因此,如果我们转到 http://serverurl ...,它将加载索引.html ,然后单击index.html上的导航链接,将用户带到 http://serverurl/route1/component1

so if we go to http://serverurl ... it loads index.html and then clicking on navigation link on index.html it takes user to http://serverurl/route1/component1

但是,当用户尝试访问url http://serverurl/route1/component1 http://serverurl/route1/component1 ,它将请求发送到IIS,并且返回并返回错误,找不到资源.

But when user tries to go to url http://serverurl/route1/component1 directly by typing http://serverurl/route1/component1 in browser address bar, it is sending that request to IIS, and that returns and error with resource not found.

而且很容易理解,该URL在服务器上不存在.那是角度URL.理想情况下,它应加载 index.html 并将其余URL传递到有角路由器,并加载/route1/component1 .并且可以与"ng serve"一起使用,但不适用于IIS 7.5.我必须在IIS中进行任何设置才能使其正常工作吗?还是我在Anuglar2 App中要做的任何事情?

And it is easy to understand, that url is not exists on server. That is angular url. Ideally, it should load index.html and pass rest of the url to angular router and load that /route1/component1. And that works with 'ng serve' but does not work with IIS 7.5. Is there any settings I have to do in IIS to get it working ? Or in anything I have to do in Anuglar2 App ?

有人可以建议我如何解决此问题吗?

Can anyone suggest me how to get this fixed?

推荐答案

我可以通过执行以下步骤来解决此问题..

I get this resolved by doing following steps ..

  1. https:下载的 URL重写器模块://www.microsoft.com/zh-cn/download/details.aspx?id=7435

  1. Downloaded URL Rewriter module from https://www.microsoft.com/en-au/download/details.aspx?id=7435

已将以下内容添加到我的web.config

Added following to my web.config

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS" 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>

  1. 在index.html中设置基本标记.它应该紧接在&head; 标记
  2. 之后
  1. Setting up base tag in index.html. It should be immediately after <head> tag

<base href="/"/>

这三个步骤将为您设置在 IIS 7.5或IIS 8.0

These three steps will set you up for deployment of Angular2 / Angular App with html5mode url on IIS 7.5 or IIS 8.0

希望这会有所帮助.我必须经历几个答案才能解决这个问题.

Hope this will help. I have to go through few answers to get this one resolved.

这篇关于在IIS上托管Angular2应用后,直接URL无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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