【发布时间】:2019-01-15 16:46:42
【问题描述】:
我正在尝试用ASP.NET Core 学习Angular,并且有一个基本的项目启动。现在我遇到了一个非常基本的问题,我猜它需要很少的修改。我创建了一个 web api 项目并将其托管在 IIS - 内部服务器中。所以我在Angular中使用了如下api:
import { Component, Inject } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Our First Angular App';
public values: object[];
constructor(private http: Http) {
this.http.get('http://192.168.10.100/api/values').subscribe(result => { //Calling the web api here as jSon
this.values = result.json() as object[];
}, error => console.error(error));
}
}
上面没有用Angular在前端获取数据。相反,当使用浏览器的检查元素进行调试时,它会抛出异常:
http://192.168.10.100/api/values - http request was insecure
我可以理解,它可能需要集成 https 或某种安全性。但就目前而言,有什么办法可以让上面的代码与 http 请求一起工作?如果有帮助,我将包括以下配置文件:
angular.json:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"OurFirstAngularApp": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "wwwroot",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css",
"C:\\Users\\AT\\Documents\\Visual Studio 2017\\Projects\\OurFirstAngularApp\\OurFirstAngularApp\\node_modules\\bootstrap\\dist\\css\\bootstrap.min.css"
],
"scripts": [
"C:\\Users\\AT\\Documents\\Visual Studio 2017\\Projects\\OurFirstAngularApp\\OurFirstAngularApp\\node_modules\\bootstrap\\dist\\js\\bootstrap.min.js"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "OurFirstAngularApp:build"
},
"configurations": {
"production": {
"browserTarget": "OurFirstAngularApp:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "OurFirstAngularApp:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"OurFirstAngularApp-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "OurFirstAngularApp:serve"
},
"configurations": {
"production": {
"devServerTarget": "OurFirstAngularApp:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "OurFirstAngularApp"
}
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
}
}
注意:上面的 api 在请求时工作正常,我的意思是完美地获取 jSon 数据。
更新 1:Startup.cs
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application,
// visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("Hello World!");
//});
}
}
更新 2:jSon 数据 - Raw jSon Data
现在我明白了。基本上它是在进行 http 调用,出于安全原因,它需要 https。所以我尝试了一个包含原始jSon 数据的 https 服务。但是当我完成以下操作时:
public default: object[];
constructor(private http: Http) {
this.http.get('https://jsonplaceholder.typicode.com/todos/1/').subscribe(result => {
this.default = result.json() as object[];
}, error => console.error(error));
}
终于在前端加上Angular:
<tr *ngFor="let value of default">
<td>{{ value.userId }}</td>
<td>{{ value.title }}</td>
</tr>
上面在使用浏览器的检查元素时抛出了异常,如下所示 - 错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到数组等 Iterables。
【问题讨论】:
-
无论是
http还是https,它都应该可以工作,我看到你标记了asp.net core,也许你在启动时有一些https 配置? -
我已经用 Startup.cs 文件 @penleychan 更新了帖子。看看这是否是我需要配置的东西。
-
startup.cs 看起来很简单,Angular 和你的 api 在
http上运行的不是混合内容吗? -
我会从错误中猜测 IIS 服务器需要一个 https 请求,而您正在发出一个 http 请求,所以那里不匹配。您是否尝试过将 this.http.get('192.168.10.100/api/values 调用更改为 this.http.get('192.168.10.100/api/values?
-
@AT-2017 您是否尝试在订阅回调中的
this.values = result.json() as object[];行设置断点以检查api 是否返回有效数据?
标签: asp.net angular asp.net-core angular7