我正在做一个组合的。Net Core 3.1,Angular 8项目。
Angular代码嵌入到包含MVC代码的Visual Studio项目中,位于ClientApp文件夹下
我同意。但从两天前开始,角度对我来说还是新的。
我试图让Angular代码调用MVC API
我一直在关注大量视频和教程,我提出这一点作为概念证明。
角码
import { Component, OnInit } from "@angular/core";
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'people-list',
templateUrl: "peopleListView.component.html"
})
export class PeopleListView implements OnInit {
constructor(private http: HttpClient) { }
ngOnInit() {
let test = "";
test = "/api/Person/GetPeopleList";
this.http.get(test).subscribe((data: any) => console.log(data), (err: any) => console.log(err));
}
MVC代码
[ApiController]
[Route("api/[controller]")]
public class PersonController : Controller
{
public PersonController()
{
}
[Route("api/[controller]/[action]")]
[HttpGet]
public List<Person> GetPeopleList()
{
List<Person> people = new List<Person>{
new Person{Id = 1, Name = "Scott"},
new Person{Id = 2, Name = "Bill"}
};
return people;
}
}
我可以看到Angular代码上的断点被击中了,所以之前的Angular代码运行正常,但是GetPeopleList()方法没有击中,我得到了一个404错误。
我尝试了许多不同的路线,例如[route(“api/[controller]]),但没有成功。
我哪里做错了?
使现代化
看到我的代码正在请求
localhost:5001/api/Person/GetPeopleList
我想知道API是否真的在端口5001上,因为它是由Angular应用程序控制的。