我的问题是,当我将有效负载主体中以零(012345)开头的任何整数值发送到C# web API时,收到的值没有第一个数字(12345)。它忽略了零。如何强制API接收原始数据?
[HttpPost]
public void insertdata([FromBody]Model model)
{
// model.id=1234
}
有效载荷
{id:01234}
发布于 2020-11-10 21:31:20
整数不能包含@Richard在注释中提到的前导零
你应该在你的模型中使用字符串。
public class YourModel {
public string id {get;set;}
}
[HttpPost]
public void insertdata(YourModel model)
{
model.id=01234
}
发布于 2020-11-10 21:27:07
我认为这取决于模型对象中id属性的DataType。
如果id是一个字符串,如01234中的前导零将被保留。假设您是,需求并不会特别限制您将id
属性设置为int
public class Model
{
public string id { get; set;}
...
}
发布于 2020-11-10 21:26:27
将任何整数值作为字符串"012345“- payload {id:"01234"}发送。另一种方法是使用string.PadLeft - model.Id.ToString(),PadLeft(5,"0");
https://stackoverflow.com/questions/64769707
复制相似问题