billbogaiv / hybrid-model-binding

Provides the ability to bind models using both ModelBinder and ValueProvider in ASP.NET Core.
MIT License
104 stars 19 forks source link

FromHeader binding #23

Closed artyom-p closed 4 years ago

artyom-p commented 5 years ago

Is it possible somehow to bind properties to header values?

billbogaiv commented 5 years ago

Not yet. HeaderModelBinder exists in the aspnet/Mvc repo., but it's for very simple situations. I don't know why Microsoft never added a HeaderValueProvider to go with the other value providers. I postponed writing my own thinking they would eventually do it...

billbogaiv commented 5 years ago

🎉 https://www.nuget.org/packages/HybridModelBinding/0.13.0

public class Person
{
    [From(Source.Header)]
    public int Age { get; set; }
}
curl -X GET \
  http://localhost/ \
  -H 'Age: 42'
artyom-p commented 5 years ago

Wow, thank you!

artyom-p commented 5 years ago

Hey, binding works perfectly, just a small proposal, it would be great to have a possibility to override a binding name.

For example i have a header - x-request-id, and by default binder is unable to bind header to property RequestId (unfortunately it's no possible to use dashes in names).

As a solution, aditional parameter for an attribute could work. This could be also helpful not only for header binding.

billbogaiv commented 5 years ago

https://www.nuget.org/packages/HybridModelBinding/0.14.0 implements a new [HybridBindProperty]-attribute that will allow you to reference a separate header-name. There are some additional features which can be found in the README.

artyom-p commented 5 years ago

That's a great news! Great update!

billbogaiv commented 4 years ago

Doing some belated house cleaning. Issue has been resolved.

Rick house-cleaning