从 WebApi 控制器方法的 Angular 服务传递参数时采用 Int 的默认值
Taking default value of Int when parameter is passed from Angular service from WebApi controller method
我是 Angular 和 WebApi 的新手。
我想使用 webApi 调用检索数据。
我正在尝试调用 Api 方法并从 angular 服务传递一个 id。
下面是服务。
retrieve-data.service.ts
GetBatchDetailsById(batchId) {
return this.httpClient.get<IPerson>(this.base_url +
'api/SampleData/GetPersonsById', batchId);
}
当我看到 webApi 方法时,参数是 int 类型,在调试时我看到值是 0。
谁能帮我看看我错过了什么?
SampleDataController.cs
[Route("api/[controller]")]
public class SampleDataController : Controller {
[HttpGet("{id:int}")]
public Person GetPersonsById(int id)
{
return SampleDataController.personList.FirstOrDefault(x=> x.Id == id);
}
}
鉴于使用的路由属性,请求需要遵循路由模板
api/SampleData/{id:int}
但是自从你使用
api/SampleData/GetPersonsById
GetPersonsById
段被用作 id
参数,因为它不是 int
,它默认为 0
使用正确的格式
相应地构建目标URL
GetBatchDetailsById(batchId) {
var url = this.base_url + 'api/SampleData/' + batchId;
return this.httpClient.get<IPerson>(url);
}
我是 Angular 和 WebApi 的新手。
我想使用 webApi 调用检索数据。
我正在尝试调用 Api 方法并从 angular 服务传递一个 id。
下面是服务。
retrieve-data.service.ts
GetBatchDetailsById(batchId) {
return this.httpClient.get<IPerson>(this.base_url +
'api/SampleData/GetPersonsById', batchId);
}
当我看到 webApi 方法时,参数是 int 类型,在调试时我看到值是 0。
谁能帮我看看我错过了什么?
SampleDataController.cs
[Route("api/[controller]")]
public class SampleDataController : Controller {
[HttpGet("{id:int}")]
public Person GetPersonsById(int id)
{
return SampleDataController.personList.FirstOrDefault(x=> x.Id == id);
}
}
鉴于使用的路由属性,请求需要遵循路由模板
api/SampleData/{id:int}
但是自从你使用
api/SampleData/GetPersonsById
GetPersonsById
段被用作 id
参数,因为它不是 int
,它默认为 0
使用正确的格式
相应地构建目标URLGetBatchDetailsById(batchId) {
var url = this.base_url + 'api/SampleData/' + batchId;
return this.httpClient.get<IPerson>(url);
}