Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Consider allowing binding header values to types other than string and string collections #5859

Closed
DaniJG opened this issue Feb 26, 2017 · 16 comments

Comments

@DaniJG
Copy link

DaniJG commented Feb 26, 2017

Right now when attempting to bind a model/property from a header, any type that isn't a string or a string collection is ignored as per the HeaderModelBinderProvider implementation.

There will be common cases where at least you might need to bind other value types like int, DateTime or Guid which will get silently ignored.

I understand binding to complex objects, collections and dictionaries might pose a challenge but at least simple value types should be supported.

@danroth27
Copy link
Member

@DaniJG Which specific headers would you want to use this for?

@ghosttie
Copy link

ghosttie commented Mar 2, 2017

I'm sending an authentication token (Guid) in the header

@DaniJG
Copy link
Author

DaniJG commented Mar 2, 2017

I found this issue while looking at a question on stackoverflow so it wasn't a specific scenario that I had in mind.

However, I could see the need for Guids as in the sessionId header or a deviceId header.

Dates could also be needed, for example with the Date header, any other standard header of type http-date or any custom headers used by your app.

@Eilon
Copy link
Member

Eilon commented Mar 3, 2017

@rynowak do you recall if there was some reason we supported only strings? Was it to avoid having errors during binding? (Because we can pretty much always bind to strings.) Or we just didn't think to do it?

@rynowak
Copy link
Member

rynowak commented Mar 3, 2017

It was just for YAGNI reasons. We wanted to wait for feedback.

@ghost
Copy link

ghost commented Jun 7, 2017

Hi guys, we really appreciate your work, perhaps I think this functionality should be implemeted. What kind of feedback you need? I'm trying a similar scenario like @DaniJG and I'm experiencing the same issue. Seems to redudant to always parse the string to a Guid struct.

@rynowak
Copy link
Member

rynowak commented Jul 17, 2017

@kellermanrivero - this is exactly the kind of feedback we were waiting for. "Yes I need this, and I want to be able to convert to a guid".

This is planned for 2.1

@rynowak rynowak closed this as completed Jul 17, 2017
@rynowak rynowak reopened this Jul 17, 2017
@billpratt
Copy link

I must say, I really enjoy and appreciate the effort to engage with the community. Cheers!

@elmar-de-groot
Copy link

elmar-de-groot commented Nov 9, 2017

Not sure if you are just implementing to support guid. But would like implementation to mimic FromQuery behavior. In my scenario I was trying to use SomeAction([FromHeader(Name = "x-page-number")] pageNumber)

@kichalla
Copy link
Member

Not sure if you are just implementing to support guid. But would like implementation to mimic FromQuery behavior. In my scenario I was trying to use SomeAction([FromHeader(Name = "x-page-number")] pageNumber)

@elmar-de-groot FromHeader already has this support.

@Marusyk
Copy link

Marusyk commented Mar 28, 2018

Will it support collections of int?

@Shamshiel
Copy link

Was this feature added to ASP.NET Core 2.1?

@Marusyk
Copy link

Marusyk commented Jun 12, 2018

@Shamshiel Yes, I've already used it

@Shamshiel
Copy link

@Marusyk So this should work?

https://localhost:44342/api/values/1?guid=1d434c71-48ab-457b-a7e2-4e76a1f6bfdb

grafik

Because it doesn't seem to work for me.

@Marusyk
Copy link

Marusyk commented Jun 12, 2018

Here you are passing guid as a query parameter but not as a header. So use [FromQuery]
What is the name of your header?
@Shamshiel Did you add?

services 
   .AddMvcCore()
   .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)

@Shamshiel
Copy link

@Marusyk Yeah you are right. It was a stupid mistake on my side. I just created a quick example and stopped thinking on what I'm actually doing. Sorry -.-"
It works if I actually send the value in the header!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests