diff --git a/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj b/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj
index 6d940eb..b6cf54e 100755
--- a/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj
+++ b/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj
@@ -3,6 +3,7 @@
netcoreapp3.1
+
diff --git a/samples/AspNetCoreWebApplication/Controllers/DataController.cs b/samples/AspNetCoreWebApplication/Controllers/DataController.cs
new file mode 100644
index 0000000..768d734
--- /dev/null
+++ b/samples/AspNetCoreWebApplication/Controllers/DataController.cs
@@ -0,0 +1,31 @@
+using System.Text.Json.Serialization;
+using HybridModelBinding;
+using Microsoft.AspNetCore.Mvc;
+using Newtonsoft.Json;
+
+namespace AspNetCoreWebApplication.Controllers
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class DataController : ControllerBase
+ {
+ [HttpPost]
+ public ActionResult Index(DataModel model)
+ {
+ return $"{model.Name} - {model.IsAdmin}";
+ }
+ }
+
+ public class DataModel
+ {
+ [HybridBindProperty(Source.Body)]
+ public string Name { get; set; }
+
+ [HybridBindProperty(Source.Body)]
+ [JsonPropertyName("is_admin"), JsonProperty("is_admin")]
+ public bool IsAdmin { get; set; }
+
+ [HybridBindProperty(Source.Header, "X-UserId")]
+ public int UserId { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/samples/AspNetCoreWebApplication/Startup.cs b/samples/AspNetCoreWebApplication/Startup.cs
index 1586411..62ba3f9 100755
--- a/samples/AspNetCoreWebApplication/Startup.cs
+++ b/samples/AspNetCoreWebApplication/Startup.cs
@@ -10,7 +10,8 @@ public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
- .AddHybridModelBinder();
+ .AddHybridModelBinder()
+ .AddNewtonsoftJson();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
@@ -27,10 +28,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseStaticFiles();
app.UseRouting();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- });
+ app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
-}
+}
\ No newline at end of file
diff --git a/src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs b/src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs
new file mode 100644
index 0000000..03910db
--- /dev/null
+++ b/src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs
@@ -0,0 +1,30 @@
+using System.Reflection;
+using System.Text.Json.Serialization;
+using Newtonsoft.Json;
+
+namespace HybridModelBinding.Extensions
+{
+ public static class PropertyInfoExtensions
+ {
+ public static string GetNameFromAttributes(this PropertyInfo propertyInfo)
+ {
+ //default value
+ var name = propertyInfo.Name;
+
+ //newtonsoft.json
+ var jsonPropertyAttribute = propertyInfo.GetCustomAttribute();
+ if (jsonPropertyAttribute != null)
+ {
+ return jsonPropertyAttribute.PropertyName;
+ }
+ //system.text.json
+ var jsonPropertyNameAttribute = propertyInfo.GetCustomAttribute();
+ if (jsonPropertyNameAttribute != null)
+ {
+ return jsonPropertyNameAttribute.Name;
+ }
+
+ return name;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/HybridModelBinding/HybridModelBinding.csproj b/src/HybridModelBinding/HybridModelBinding.csproj
index bd6d87f..a193978 100755
--- a/src/HybridModelBinding/HybridModelBinding.csproj
+++ b/src/HybridModelBinding/HybridModelBinding.csproj
@@ -22,6 +22,7 @@
runtime; build; native; contentfiles; analyzers; buildtransitive
all
+
diff --git a/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs b/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs
index 8fd6f8f..61bd0c9 100755
--- a/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs
+++ b/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs
@@ -41,10 +41,13 @@ public BodyValueProvider(
.ToArray();
}
- foreach (var property in model.GetPropertiesNotPartOfType()
- .Where(x => !requestKeys.Any() || requestKeys.Contains(x.Name, StringComparer.OrdinalIgnoreCase)))
+ foreach (var property in model.GetPropertiesNotPartOfType())
{
- values.Add(property.Name, property.GetValue(model, null));
+ var propertyName = property.GetNameFromAttributes();
+ if (!requestKeys.Any() || requestKeys.Contains(propertyName, StringComparer.OrdinalIgnoreCase))
+ {
+ values.Add(property.Name, property.GetValue(model, null));
+ }
}
}
@@ -78,4 +81,4 @@ public object GetObject(string key)
public ValueProviderResult GetValue(string key) => throw new NotImplementedException($"Use `{nameof(GetObject)}`.");
}
-}
+}
\ No newline at end of file