如何配置systemjs.config.js以在IIS中使用相对路径?

时间:2017-10-19 22:32:45

标签: asp.net-mvc angular typescript systemjs

我刚刚启动了一个ASP.NET MVC Web应用程序,它使用Angular作为其UI组件。将必要的配置文件发送到我的项目中(例如package.jsonsystemjs.config.js)。创建测试页Index.cshtml以测试app.component.ts模板。在我的localhost下运行时,应用程序正在运行。我可以看到从模板中呈现自定义标记<user-app>的页面。

但问题是,当我将Web应用程序部署到IIS(开发服务器)时,我遇到了以下HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier)

enter image description here

这是应用package.json文件

    {
  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "build": "tsc -p src/",
    "build:watch": "tsc -p src/ -w",
    "build:e2e": "tsc -p e2e/",
    "serve": "lite-server -c=bs-config.json",
    "serve:e2e": "lite-server -c=bs-config.e2e.json",
    "prestart": "npm run build",
    "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
    "pree2e": "npm run build:e2e",
    "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
    "preprotractor": "webdriver-manager update",
    "protractor": "protractor protractor.config.js",
    "pretest": "npm run build",
    "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
    "pretest:once": "npm run build",
    "test:once": "karma start karma.conf.js --single-run",
    "lint": "tslint ./src/**/*.ts -t verbose"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@angular/common": "~4.3.4",
    "@angular/compiler": "~4.3.4",
    "@angular/core": "~4.3.4",
    "@angular/forms": "~4.3.4",
    "@angular/http": "~4.3.4",
    "@angular/platform-browser": "~4.3.4",
    "@angular/platform-browser-dynamic": "~4.3.4",
    "@angular/router": "~4.3.4",

    "angular-in-memory-web-api": "~0.3.0",
    "systemjs": "0.19.40",
    "core-js": "^2.4.1",
    "rxjs": "5.0.1",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "concurrently": "^3.2.0",
    "lite-server": "^2.2.2",
    "typescript": "~2.1.0",

    "canonical-path": "0.0.2",
    "tslint": "^3.15.1",
    "lodash": "^4.16.4",
    "jasmine-core": "~2.4.1",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~4.0.14",
    "rimraf": "^2.5.4",

    "@types/node": "^6.0.46",
    "@types/jasmine": "2.5.36"
  },
  "repository": {}
}

应用systemjs.config.js文件

    /**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            'app': 'app',

            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

            // other libraries
            'rxjs': 'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
                //meta: {
                //    './*.js': {
                //        loader: 'systemjs-angular-loader.js'
                //    }
                //},
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

app.component.ts文件

    import { Component } from '@angular/core';

@Component({
    selector: 'user-app',
    template: `
        <div>
            <nav class='navbar navbar-inverse'>
                <div class='container-fluid'>
                    <ul class='nav navbar-nav'>
                        <li>
                            <a [routerLink]="['home']">Home</a>
                        </li>
                    </ul>
                </div>
            </nav>
            <div class='container'>
                <router-outlet></router-outlet>
            </div>
        </div>
    `
})
export class AppComponent {}

main.ts文件

    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

app.module.ts文件

    import { NgModule } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { HomeComponent } from './Components/home.component';

@NgModule({
    imports: [BrowserModule, ReactiveFormsModule, HttpModule, routing],
    declarations: [AppComponent, HomeComponent],
    providers: [{ provide: APP_BASE_HREF, useValue: '/' }],
    bootstrap: [AppComponent]
})
export class AppModule {}

和app页面_Layout.cshtml文件

    <!DOCTYPE html>
<html>
<head>
    <base href="/" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Control Systems Management</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <script src="~/node_modules/core-js/client/shim.min.js"></script>
    <script src="~/node_modules/zone.js/dist/zone.js"></script>
    <script src="~/node_modules/systemjs/dist/system.src.js"></script>
    <script src="~/systemjs.config.js"></script>
    <script>
        //SystemJS.config({
        //    baseURL: '.'
        //});

        System.import('app').catch(function (err) {
            console.error(err);
        });
    </script>
</head>
<body>
    <div class="container body-content">
        @RenderBody()
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

同样在IIS中,我已经配置了一个应用程序,并在ControlSystem下将其命名为Default Web Site。查看http error,服务器找不到main.js,因为它被认为是相对路径(例如http://myDomain/controlsystem/app/main.js)。我尝试了几种方法来配置systemjs.config.js,但它没有成功。

这是我的visual studio项目中的.ts个文件的文件夹结构

enter image description here

我在网上做了一些关于如何解决这个问题的研究,但我找不到类似的问题。如果有,请指导我这样的参考。

我非常擅长Angular并在线浏览以了解所有这些相关的配置设置。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

为了可能遇到此问题的其他开发人员的利益。幸运的是,我找到了解决方案。

这是一个非常简单的解决方案。在_Layout.cshtml<head>部分,将<base href="~/" />作为第一个元素。

根据此文档https://angular.io/guide/router<base href="/">告诉Angular路由器URL的静态部分是什么。然后,路由器仅修改URL的剩余部分。在我的情况下,我不得不把~“代替”以使其相对。

希望这会有所帮助。