Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Created November 13, 2021 10:44
Show Gist options
  • Save gistlyn/14b6cd51cd3fb64988e538f684d42f7c to your computer and use it in GitHub Desktop.
Save gistlyn/14b6cd51cd3fb64988e538f684d42f7c to your computer and use it in GitHub Desktop.
HeaderMappingTest
<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>
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