Created
November 13, 2021 10:44
-
-
Save gistlyn/14b6cd51cd3fb64988e538f684d42f7c to your computer and use it in GitHub Desktop.
HeaderMappingTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<NoWarn>1591</NoWarn> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="ServiceStack.Common" Version="5.*" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using ServiceStack; | |
using ServiceStack.Text; | |
var headers = new NameValueCollection { | |
[nameof(RequestDto.AName)] = "updated", | |
[nameof(RequestDto.BName)] = "updated", | |
}; | |
var dto = new RequestDto { | |
AName = "original", | |
BName = "original", | |
}; | |
// GlobalRequestFilters.Add((req,res,dto) => {... | |
var populateValues = new Dictionary<string, object>(); | |
typeof(RequestDto).GetProperties() | |
.Where(x => x.HasAttribute<FromHeaderAttribute>()) | |
.Each(x => populateValues[x.Name] = headers[x.Name]); | |
populateValues.PopulateInstance(dto); | |
// }); | |
dto.PrintDump(); | |
/* Output: | |
{ | |
AName: original, | |
BName: updated | |
}*/ | |
public class FromHeaderAttribute : Attribute {} | |
public class RequestDto | |
{ | |
public string AName { get; set; } | |
[FromHeader] | |
public string BName { get; set; } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment