Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.23" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
Expand Down
31 changes: 31 additions & 0 deletions samples/AspNetCoreWebApplication/Controllers/DataController.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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; }
}
}
10 changes: 4 additions & 6 deletions samples/AspNetCoreWebApplication/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddHybridModelBinder();
.AddHybridModelBinder()
.AddNewtonsoftJson();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand All @@ -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(); });
}
}
}
}
30 changes: 30 additions & 0 deletions src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<JsonPropertyAttribute>();
if (jsonPropertyAttribute != null)
{
return jsonPropertyAttribute.PropertyName;
}
//system.text.json
var jsonPropertyNameAttribute = propertyInfo.GetCustomAttribute<JsonPropertyNameAttribute>();
if (jsonPropertyNameAttribute != null)
{
return jsonPropertyNameAttribute.Name;
}

return name;
}
}
}
1 change: 1 addition & 0 deletions src/HybridModelBinding/HybridModelBinding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>
<Target Name="MinVerPreReleaseCheck" AfterTargets="MinVer" Condition="$(MinVerPreRelease.Split('.').Length) == 3">
<PropertyGroup>
Expand Down
11 changes: 7 additions & 4 deletions src/HybridModelBinding/ModelBinding/BodyValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ public BodyValueProvider(
.ToArray();
}

foreach (var property in model.GetPropertiesNotPartOfType<IHybridBoundModel>()
.Where(x => !requestKeys.Any() || requestKeys.Contains(x.Name, StringComparer.OrdinalIgnoreCase)))
foreach (var property in model.GetPropertiesNotPartOfType<IHybridBoundModel>())
{
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));
}
}
}

Expand Down Expand Up @@ -78,4 +81,4 @@ public object GetObject(string key)

public ValueProviderResult GetValue(string key) => throw new NotImplementedException($"Use `{nameof(GetObject)}`.");
}
}
}