【发布时间】:2017-10-23 16:56:25
【问题描述】:
晚安社区,
我在从 Angular 2 POST 方法将数据发布到 C# Api 时遇到问题。实际上,我需要向 API 发送一个 JSON 对象。下面是作为端点的 API POST 方法。
[HttpPost]
public Boolean Update(BoRole role, Guid accessToken)
{
CheckUserSuperadmin(accessToken);
string JsonContent = Request.Content.ReadAsStringAsync().Result;
var roleEntity = BaseDependencies.RoleManager.GetRoleById(role.Id);
roleEntity.Name = role.Name;
roleEntity.AuthorizedThreshold = role.AuthorizedThreshold;
BaseDependencies.RoleManager.UpdateRole(roleEntity);
return true;
}
以下是我的 Angular 2 服务,使用 POST 方法。
// Update the Role
updateRole(role: Role, accessToken: string): any {
const body: any = JSON.stringify(role);
const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({ headers: headers });
const url = this.serverUrl + '/api/Role/Update?role=' + body + '&accessToken=' + accessToken;
return this.http.post(url, body, options).map((res: Response) => res.json());
}
为了更好地解释,我正在尝试将 JSON 对象发送到此 API,但每当我尝试向 API 发布时,role 对象仍然为空,从未尝试过以这种方式发布数据。有可能吗?
谢谢
【问题讨论】: