donet (MVC)webAPI 的接受json 的操作

直接用对象来进行接收,这个方法还不错的。

csharp 复制代码
    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);
        }
  }

调用方法就是:

csharp 复制代码
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);
相关推荐
UpUpUp……16 小时前
Linux--JsonCpp
linux·运维·服务器·c++·笔记·json
wtsolutions1 天前
Excel-to-JSON插件专业版功能详解:让Excel数据转换更灵活
json·excel·excel-to-json·wtsolutions·专业版
kerryYG1 天前
使用JMETER中的JSON提取器实现接口关联
jmeter·json
柯ran2 天前
JSON|cJSON 介绍以及具体项目编写
c语言·链表·json·github
乐言3613 天前
Jmeter中的Json提取器如何使用?
jmeter·json
北海有初拥3 天前
【从零实现JsonRpc框架#1】Json库介绍
json
GalenWu4 天前
对象转换为 JSON 字符串(或反向解析)
前端·javascript·微信小程序·json
致于数据科学家的小陈4 天前
Go 层级菜单树转 json 处理
python·go·json·菜单树·菜单权限·children
我爱夜来香A4 天前
SQL进阶:如何把字段中的键值对转为JSON格式?
数据库·sql·json
Mr.小怪4 天前
Jackson中的ObjectNode与JsonNode详解
json