donet (MVC)webAPI 的接受json 的操作
直接用对象来进行接收,这个方法还不错的。
public class BangdingWeiguiJiluController : ApiController
{
/// <summary>
/// Json数据录入错误信息
/// </summary>
/// <param name="WeiguiInfos"></param>
/// <returns></returns>
[System.Web.Http.HttpPost]
public JsonResult<ResultMsgWeb> WeiguiJiluAdd([FromBody]BangdingWeiguiJilu WeiguiInfos )
{
//BangdingWeiguiJilu modelInf = new BangdingWeiguiJilu() { SetDate= DateTime.Now,YuangongId= 2756,SetType=1,Creator = 9999999 };
//string Infr = JsonConvert.SerializeObject(modelInf);
ResultMsgWeb Msg = new ResultMsgWeb();
try
{
ResultMsg MsgInsert = new DAL.DALBangdingWeiguiJilu().Insert(WeiguiInfos);
if (MsgInsert.Success)
{
Msg.Code = 200;
Msg.Datas = "处理完成" ;
Msg.Msg = $"处理完成";
Msg.RecordCount = 0;
}
else
{
Msg.Code = 400;
Msg.Datas = "数据处理失败";
Msg.Msg = $"数据处理失败";
Msg.RecordCount = 0;
}
}
catch (System.Exception e)
{
Msg.Code = 404;
Msg.Datas = "数据处理失败";
Msg.Msg = $"数据处理失败" + e.Message;
Msg.RecordCount = 0;
}
return Json<ResultMsgWeb>(Msg);
}
}
调用方法就是:
var client = new RestClient("http://localhost:56727/api/BangdingWeiguiJilu/WeiguiJiluAdd");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
client.UserAgent = "Apifox/1.0.0 (https://apifox.com)";
request.AddHeader("Content-Type", "application/json");
var body = @"<body data here>";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);