如何在 HTTP GET 请求中传递一个简单的整数参数?
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);
});
}
我的组件中有以下代码,它应该向 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);
});
}