.net core下JObject接收参数问题

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

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

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

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

接口代码实现如下:


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

2、方案二 (使用JObject接收参数)
.net core 处理方式是转为强类型,没有对应的强类型会被抛弃,以下使用自定义处理方法,
.net core下JObject接收参数问题_第1张图片
(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绑定
            });

你可能感兴趣的:(技术分享)