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);
相关推荐
深海蓝山1 天前
package.json 依赖包约束及快速删除node_modules
json
轻口味2 天前
【每日学点HarmonyOS Next知识】获取资源问题、软键盘弹起、swiper更新、C给图片设置位图、读取本地Json
c语言·json·harmonyos·harmonyosnext
DarkLONGLOVE2 天前
一文读懂 XML 文档:概念、结构与应用场景!
前端·json
Newfocus!3 天前
GPU算力哪家好?GpuGeek推出高性能GPU云服务
json
故事里故去3 天前
.net 6.0 webapi支持 xml返回xml json返回json
xml·json·.net
Java陈序员4 天前
IDEA 必备插件!轻松搞定 JSON 格式化!
java·json·intellij idea
trabecula_hj5 天前
SpreadVue实现内置excel在线编辑并保存为后端可以接受的json格式
json·excel·spreadjs·spreadvue
阿珊和她的猫5 天前
SyntaxError: Invalid or unexpected token in JSON at position x
json·状态模式
Tipriest_5 天前
vscode user settings.json分享
ide·vscode·json
weixin_461769407 天前
将JSON格式数据转化为sql 插入语句
python·sql·json