我正在尝试将Angular-8与WebAPI-2结合使用,但出现500状态代码错误。在ASP.Net MVC中尝试了相同的服务代码,并且工作正常。
Web API代码:
TestController.cs
public IHttpActionResult Get()
{
return Ok("Hello from test web api");
}
角度代码:
test.service.ts
export class TestService {
constructor(private http: HttpClient) { }
getTestMessage(){
return this.http.get('http://localhost:5638/api/test');
}
}
test.component.ts
export class TestComponent implements OnInit {
testMessage: any;
constructor(private testService: TestService) { }
ngOnInit() {
this.testService.getTestMessage().subscribe(
testMessage => {
this.testMessage = testMessage;
}
);
}
}
错误:
GET http://localhost:5638/api/test 500 (Internal Server Error)
ERROR HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "Internal Server Error", url: "http://localhost:5638/api/test", ok: false, …}