通过传递单个参数进行Angular WebApi Post调用

问题描述

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});                   
    }

我收到 404未找到方法错误

我还尝试过使用 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方法未找到错误。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...