Files
zszq-trs/Framework/SharedWebApi/AspNetCustom/JsonBodyBinderProvider.cs
T
2024-05-09 14:06:26 +08:00

52 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Newtonsoft.Json;
namespace YLWebAPI.ApiModule
{
/// <summary>
/// application/json模型绑定(支持空body,避免报400 error:A non-empty request body is required.)
/// </summary>
internal class JsonBodyBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (!context.Metadata.ModelType.IsValueType)
{
return new BinderTypeModelBinder(typeof(EmptyBodyModelBinder));
}
return null;
}
class EmptyBodyModelBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext.HttpContext.Request.ContentType?.Contains("application/json") != true)
{
return;
}
var stream = bindingContext.HttpContext.Request.Body;
using var reader = new StreamReader(stream);
var jsonbody = await reader.ReadToEndAsync();
if (jsonbody.Length > 0)
{
var obj = JsonConvert.DeserializeObject(jsonbody, bindingContext.ModelType);
bindingContext.Result = ModelBindingResult.Success(obj);
}
else
{
bindingContext.Result = ModelBindingResult.Success(null);
}
}
}
}
}