OpenAuth.Net/OpenAuth.Mvc/Models/JobjectModelBinder.cs
2020-10-22 14:59:36 +08:00

39 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Newtonsoft.Json.Linq;
namespace OpenAuth.Mvc.Models
{
/// <summary>
/// 将前端传来的FormData数据转为Jobject类型
/// 注前端如果是application/json可以直接转JOjbect
/// </summary>
public class JobjectModelBinder :IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
var obj = new JObject();
//// Specify a default argument name if none is set by ModelBinderAttribute
//var modelName = bindingContext.BinderModelName;
//if (string.IsNullOrEmpty(modelName))
//{
// modelName = "obj";
//}
//// Try to fetch the value of the argument by name
//var valueProviderResult =
// bindingContext.ValueProvider.GetValue(modelName);
//这个地方会报StringValues的异常好奇怪只能调试源码了
var request = bindingContext.HttpContext.Request;
foreach (var item in request.Form)
{
obj[item.Key] = item.Value[0];
}
bindingContext.Result = ModelBindingResult.Success(obj);
return Task.CompletedTask;
}
}
}