恒石成品库WCS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace WcfControlMonitorWebLib
{
/// <summary>
/// 指定参数仅来自传入的实体成员
/// </summary>
[AttributeUsage(AttributeTargets.Parameter)]
public class FromMemberAttribute : ModelBinderAttribute
{
/// <summary>
/// 构造函数
/// </summary>
public FromMemberAttribute() : base(typeof(ModelBinder)) { }
/// <summary>
/// 实体绑定器
/// </summary>
private class ModelBinder : IModelBinder
{
/*
* 实现 Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder.BindModelAsync(ModelBindingContext bindingContext) 方法
* add for TJBS
*/
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var json = string.Empty;
using (var reader = new StreamReader(bindingContext.HttpContext.Request.Body))
{
json = await reader.ReadToEndAsync();
}
var value = JsonHelper.Deserialize<JObject>(json)[bindingContext.FieldName];
if (value != null)
{
bindingContext.Result = ModelBindingResult.Success(value.ToObject(bindingContext.ModelType));
}
bindingContext.HttpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(json));
}
}
}
}