Angular 2 Hosted on IIS: HTTP Error 404

前端 未结 4 1613
北荒
北荒 2020-12-13 07:05

I have simple app with one component that expects certain parameters from url. there is only one route in the application:

const appRoutes: Routes = 
                


        
相关标签:
4条回答
  • 2020-12-13 07:43

    We have to make IIS fall back to index.html by adding a rewrite rule.

    Step 1: Install IIS URL Rewrite Module

    Step 2: Add a rewrite rule to 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 讨论(0)
  • 2020-12-13 07:46

    If you are using an IIS application like myApp below your default web site (then the URL to your application should have the syntax http://localhost/myApp/my-angular-route), then you should try to modify the action to

    <action type="Rewrite" url="/myApp/"/>;
    

    Then it works for my environment without any problems.

    Step by step instructions can be found here: https://blogs.msdn.microsoft.com/premier_developer/2017/06/14/tips-for-running-an-angular-app-in-iis/

    0 讨论(0)
  • 2020-12-13 07:50

    STEP 3: add web.config link to angular.json: (because after ng build it will skipped)

    "architect": {
                    "build": {
                        "builder": "@angular-devkit/build-angular:browser",
                        "options": {
                             ..........
                            "assets": [
                                "src/favicon.ico",
                                "src/assets",
                                **"src/web.config"**
                            ]
    
    0 讨论(0)
  • 2020-12-13 07:54

    Modify angular routing by adding # after baseurl:

    http://localhost:4200/yourApp#/subPageRoute

    In app-routing.module.ts on line with RouterModule.forRoot add ", { useHash: true }"

    RouterModule.forRoot(routes, { useHash: true })
    

    This way IIS thinks everything after # is some id on page and ignores it.

    0 讨论(0)
提交回复
热议问题