2016-12-14 99 views
2

我有一個項目的asp.net-mvc與angular2元素。用於啓動iis應用程序,該應用程序在Home文件夾中啓動index.cshtml(由家庭控制器啓動)。所以,我修改的角度,具體如下,索引文件:角度2 system.import錯誤的路徑('應用程序')

<!DOCTYPE html> 
<html> 
<head> 
    <title>Angular QuickStart</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link href="~/styles.css" rel="stylesheet" /> 
    <!-- Polyfill(s) for older browsers --> 
    <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/reflect-metadata/Reflect.js"></script> 
    <script src="~/node_modules/systemjs/dist/system.src.js"></script> 
    <script src="~/systemjs.config.js"></script> 
    <script> 
     System.import('app').catch(function(err){ console.error(err); }); 
    </script> 
</head> 
<body> 
    <my-app>Loading AppComponent content here ...</my-app> 
</body> 
</html> 

現在,當我因爲做這個請求啓動該應用程序System.import('app')沒有找到app文件夾:

zone.js:1382 GET http://localhost/app/main.js 404(未找到)

但是如果我手動調用家庭控制器,正確顯示的路徑:

http://localhost/my_virtual_path/app/main.js

爲什麼這種行爲? 我可以更改system.import,以便查看相對於項目級別的路徑嗎?

的完整性,這是system.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', 

     // librerie di terze parti 
     'angular2-datatable': 'npm:angular2-datatable', 
     'lodash': 'npm:lodash/lodash.js', 
    }, 
    // packages tells the System loader how to load when no filename and/or no extension 
    packages: { 
     app: { 
     main: './main.js', 
     defaultExtension: 'js' 
     }, 
     rxjs: { 
     defaultExtension: 'js' 
     }, 


     'angular2-datatable': { 
      main: 'index.js', 
      defaultExtension: 'js' 
     }, 

    } 
    }); 
})(this); 
+0

後systemjs.config.js –

+0

的內容我加入,但我不知道認爲這很有幫助,因爲我不想改變相對路徑。 – FeroX

回答

1

首先,我強烈建議你不要使用asp.net的MVC或類似的東西的角度應用。不需要它。你可以像休息服務一樣使用它(asp.net web api)。無論如何,我認爲這個問題是來自應用程序的路徑(IIS應用程序) 所以你需要這個標記添加到HTML文件頭:

<head> 
<base href="/my_virtual_path/"> 
..... 
+0

解決這個問題,但是,我正在尋找一種相對路徑的解決方案,以避免在與同事共享文件時不得不更改文件。 – FeroX

+0

你不能像asp.net那樣做。因爲asp.net在服務器上呈現頁面。並注意你不需要使用(/ my_virtual_path /),因爲你的路徑從根(www.domain.com)開始。不要忘記在Angular2應用中需要基本標籤()(請參閱參考資料)。這是你的完整描述。 –

+0

並改變主意,忘記asp.net方法。無需在angularjs,angular2和..... apps中進行服務器端渲染。 –

相關問題