懒得勤快 3 年之前
父节点
当前提交
53bc4db84f

+ 1 - 1
Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj

@@ -18,7 +18,7 @@
         <LangVersion>latest</LangVersion>
         <LangVersion>latest</LangVersion>
         <RepositoryType>Github</RepositoryType>
         <RepositoryType>Github</RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-        <Version>1.0.2</Version>
+        <Version>1.0.3</Version>
         <FileVersion>1.0</FileVersion>
         <FileVersion>1.0</FileVersion>
         <Company>masuit.com</Company>
         <Company>masuit.com</Company>
         <AssemblyVersion>1.0</AssemblyVersion>
         <AssemblyVersion>1.0</AssemblyVersion>

+ 17 - 0
Masuit.Tools.AspNetCore/ModelBinder/BodyOrDefaultBindingSource.cs

@@ -0,0 +1,17 @@
+using Microsoft.AspNetCore.Mvc.ModelBinding;
+
+namespace Masuit.Tools.AspNetCore.ModelBinder;
+
+public class BodyOrDefaultBindingSource : BindingSource
+{
+    public static readonly BindingSource BodyOrDefault = new BodyOrDefaultBindingSource("BodyOrDefault", "BodyOrDefault", true, true);
+
+    public BodyOrDefaultBindingSource(string id, string displayName, bool isGreedy, bool isFromRequest) : base(id, displayName, isGreedy, isFromRequest)
+    {
+    }
+
+    public override bool CanAcceptDataFrom(BindingSource bindingSource)
+    {
+        return bindingSource == Body || bindingSource == this;
+    }
+}

+ 1 - 1
Masuit.Tools.AspNetCore/ModelBinder/BodyOrDefaultModelBinderProvider.cs

@@ -26,7 +26,7 @@ public class BodyOrDefaultModelBinderProvider : IModelBinderProvider
 
 
     public IModelBinder GetBinder(ModelBinderProviderContext context)
     public IModelBinder GetBinder(ModelBinderProviderContext context)
     {
     {
-        if (context.BindingInfo.BindingSource != null && context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Body))
+        if (context.BindingInfo.BindingSource != null && (context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Body) || context.BindingInfo.BindingSource.CanAcceptDataFrom(BodyOrDefaultBindingSource.BodyOrDefault)))
         {
         {
             var bodyBinder = _bodyModelBinderProvider.GetBinder(context);
             var bodyBinder = _bodyModelBinderProvider.GetBinder(context);
             var complexBinder = _complexDataModelBinderProvider.GetBinder(context);
             var complexBinder = _complexDataModelBinderProvider.GetBinder(context);

+ 10 - 0
Masuit.Tools.AspNetCore/ModelBinder/FromBodyOrDefaultAttribute.cs

@@ -0,0 +1,10 @@
+using System;
+using Microsoft.AspNetCore.Mvc.ModelBinding;
+
+namespace Masuit.Tools.AspNetCore.ModelBinder;
+
+[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
+public class FromBodyOrDefaultAttribute : Attribute, IBindingSourceMetadata
+{
+    public BindingSource BindingSource => BodyOrDefaultBindingSource.BodyOrDefault;
+}