带有Web API错误的Angular HttpErrorResponse {标头:HttpHeaders,状态:500,statusText:“内部服务器错误”

时间:2019-12-12 17:10:48

标签: angular asp.net-mvc asp.net-web-api2 http-status-code-500

我正在尝试将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, …}

0 个答案:

没有答案