.net core下JObject接收参数问题

1、解决方案一(示例)
(1)、使用MVC对象属性自动映射的方法
(2)、请求接口参数(json数据格式):

{
  "GUID": "30834419-85D3-4DD9-A7C9-B5A23D938E35",
  "BILLNO": "DSKJLSKD2019091101",
  "isOverStatus": "审批中"
}

(3)、net core下接口接收参数方法
申明对应实体类对象,ReceiveBillNo
注意如果请求方式为form表单提交,不要加 [FromBody]

/// <summary>
    /// 接收更新单号数据
    /// </summary>
    public class ReceiveBillNo
    {
        /// <summary>
        /// 单据主键
        /// </summary>
        public string GUID { get; set; }
        /// <summary>
        /// 单号
        /// </summary>
        public string BILLNO { get; set; }
        /// <summary>
        /// 单据状态
        /// </summary>
        public string isOverStatus { get; set; }
    }

接口代码实现如下:


        [Route("SetBillNoStatusByPK")]
        [HttpPost]
        public void SetBillNoStatusByPK(ReceiveBillNo _ReceiveBillNo)
        {
            ......
        }

2、方案二 (使用JObject接收参数)
.net core 处理方式是转为强类型,没有对应的强类型会被抛弃,以下使用自定义处理方法,
在这里插入图片描述
(1) 定义 ModelBinder,继承IModelBinder接口

using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

 public class JObjectModelBinder: IModelBinder
    {
        public JObjectModelBinder(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
        }
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null) throw new ArgumentNullException("bindingContext");
            ValueProviderResult result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            try
            {
                JObject obj = new JObject();
                if (bindingContext.ModelType == typeof(JObject))
                {
                    foreach (var item in bindingContext.ActionContext.HttpContext.Request.Form)
                    {
                        obj.Add(new JProperty(item.Key.ToString(), item.Value.ToString()));
                    }
                    if ((obj.Count == 0))
                    {
                        bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, bindingContext.ModelMetadata.ModelBindingMessageProvider.ValueMustNotBeNullAccessor(result.ToString()));
                        return Task.CompletedTask;
                    }
                    bindingContext.Result = (ModelBindingResult.Success(obj));
                    return Task.CompletedTask;
                }
                return Task.CompletedTask;
            }
            catch (Exception exception)
            {
                if (!(exception is FormatException) && (exception.InnerException != null))
                {
                    exception = ExceptionDispatchInfo.Capture(exception.InnerException).SourceException;
                }
                bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, exception, bindingContext.ModelMetadata);
                return Task.CompletedTask;
            }
        }
    }

2、定义 ModelBinderProvider 继承ModelBinder

using Microsoft.AspNetCore.Mvc.ModelBinding;

public class JObjectModelBinderProvider: IModelBinderProvider
    {
        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (context.Metadata.ModelType == (typeof(JObject)))
            {
                return new JObjectModelBinder(context.Metadata.ModelType);
            }
            return null;
        }
    }

3、在 Startup文件中加入JobjectModelBinderProvider绑定

services.AddMvc(options =>
            {
                options.ModelBinderProviders.Insert(0, new JObjectModelBinderProvider());//加入JobjectModelBinderProvider绑定
            });
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大海中一粒沙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值