![](/img/trans.png)
[英]I want to pass the _id as parameter in http get request in Angular
[英]How to pass a simple integer parameter in HTTP GET request?
我的组件中有以下代码,它应该向 WebApiController 发送一个 id 并取回一些数据。 但我不能简单地将参数传递给方法。 我在这里和那里尝试了许多可用的解决方案,但没有成功。
getUser(id){
this._http.get('http://localhost:53226/api/UserAPI/GetSpecificUser', id) // is there a simple way to do this?
.subscribe((result: any) => {
this.specificuser = result.json();
console.log(this.specificuser);
});
}
编辑:
这是我的控制器中的方法。
public UserModel GetSpecificUser(int id)
{
var user = UserDB.Users.FirstOrDefault(x => x.id == id);
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<User, UserModel>();
});
IMapper iMapper = config.CreateMapper();
var destination = iMapper.Map<User, UserModel>(user);
return destination;
}
如果你的 API 像http://localhost:53226/api/UserAPI/GetSpecificUser/{userId}
使用这个
this._http.get(`http://localhost:53226/api/UserAPI/GetSpecificUser/${id}`)
(或类似技术)
如果我没听错你的问题,那么我会告诉你通过使用将 id 转换为整数
this._http.get('http://localhost:53226/api/UserAPI/GetSpecificUser',parseInt(id))
希望能帮到你。
使用 post() 可能会有所帮助
getUser(id){
this._http.post('http://localhost:53226/api/UserAPI/GetSpecificUser', id)
.pipe(map(result: any) => {
this.specificuser = result.json();
console.log(this.specificuser);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.