问题描述
Web Api控制器方法
[Route("change-gross-profit")]
[HttpPost]
public IHttpActionResult UpdateGrossprofit(decimal grossprofit)
{
var customerGrossprofit = grossprofit / 100;
Context.UpdateCustomerCost();
return Ok();
}
Angular Http Call
public updateGrossprofit(grossprofit: number): Observable<void> {
const url = UrlTemplate.populate('api/customer/total','change-gross-profit',{});
return this._http.post<void>(url,grossprofit,{withCredentials: true});
}
我还尝试过使用 JSON.stringify({'grossprofit':grossprofit}) (获取不受支持的内容类型错误。) >
当前的解决方法是,我可以创建一个具有必需属性的类,然后将该类实例作为主体传递,我知道它将起作用。但是我想避免使用它,因为只有一个参数要传递给POST控制器方法。
有什么主意,如何在不创建自定义对象的情况下将单个参数作为参数从Angular传递给Post控制器方法?
解决方法
您传递的数字很好。 我在下面的代码中尝试了同样的方法,并且效果很好。
let no : number =5;
this.http.post<any>("https://xxxxx/postDecimal",no).subscribe(result => {
console.log(result);
});
这是我的后端代码:
[HttpPost("postDecimal")]
[ProducesResponseType(typeof(int),200)]
[ProducesResponseType(typeof(string),400)]
[ProducesResponseType(typeof(string),403)]
public async Task<IActionResult> PostDecimal(decimal x)
{
return Ok(x);
}
您的网址正常吗?由于出现404方法未找到错误。