Angular 2/4:如何通过带有参数的Http Post调用Web Apis?

时间:2018-01-04 13:38:31

标签: asp.net angular asp.net-web-api

我是关于Angular的新手。我看过几个地方,大多数例子都非常复杂。有什么简单的东西可以从我开始吗?调用web api的东西,它接受2个参数并返回一个对象

[HttpPost("GetHomePageData")]
        public HomePageData GetHomePageData(int P1, int P2)
        {
            //
        }

public class HomePageData
    {
        public int AddressCount { get; set; }        
    }

1 个答案:

答案 0 :(得分:1)

Angular's official documentation中,您可以看到以下示例:

@Component(...)
export class MyComponent implements OnInit {

  results: string[];

  // Inject HttpClient into your component or service.
  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    // Make the HTTP request:
    this.http.get('/api/items').subscribe(data => {
      // Read the result field from the JSON response.
      this.results = data['results'];
    });
  }
}