2017-05-04 140 views
3

我有一个组件的简单应用程序,它需要来自url的某些参数。只有一个在申请途径:Angular 2托管在IIS上:HTTP错误404

const appRoutes: Routes = 
         path: 'hero/:userId/:languageId',component: HeroWidgetComponent }]; 

在中的index.html,我有这样的标题<base href="/">

我使用的WebPack和运行应用程序的开发环境不错,浏览网址时:http://localhost:4000/hero/1/1

但是,在构建生产应用程序并获取分发文件时,然后将其托管在IIS上。

HTTP Error 404.0 - Not Found 
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. 

应用程序工作正常,如果我删除所有的路由,只是浏览:http:localhost:4200上的IIS尝试浏览时相同的URL我收到以下错误。

+1

你'IIS'服务器需要让所有的角路由重定向到'index.html'正确配置(或HTML文件,其中角应用被触发)。不幸的是,我不是'IIS'服务器专家,所以我不能给你一个真正的答案如何做到这一点。 – tftd

回答

6

我们必须通过添加重写规则使IIS回退到index.html。

步骤1: 安装IIS URL Rewrite模块

第2步: 添加重写规则web.config中

<?xml version="1.0" encoding="utf-8"?> 
    <configuration> 
     <system.webServer> 
     <rewrite> 
      <rules> 
      <rule name="AngularJS Routes" 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> 
+0

我在Godaddy上托管。我遵循这一点,但不必安装IIS URL重写,它只是工作。 – mapussah

+0

@mapussah,可能是因为该模块已安装。 – Coding

+0

嗨,我遵循同样的步骤,但它仍然会在我的本地Iis中发生404错误。我在回答中从给定的链接下载了URL重写,执行后说已安装。你能分享我失踪的内容吗?我使用Angular 4,@ angular/cli,C#Web API,VS 2015开发应用程序。 –