(See
this question on stackoverflow for additional background.)
I have a RESTful Web Service which supports CORS implemented with ASP.NET Web API. I am trying to transparently add JSON-P support without changing my controllers. I have most of this accomplished via a) a custom MediaTypeFormatter that handles outputting
of JSON/JSON-P depending on the presence of a query parameter, and b) a custom IHttpActionSelector that routes to the Get, Post, etc. methods based on the value of another query parameter if it is present, otherwise default action selection.
The last piece that I have is being able to deserialize the query string into my action method POCO parameter without adding the [FromUrl] attribute, since I'd like my controllers to be just plain RESTful controllers and let the framework handle the difference
between a regular/CORS request and a JSON-P request:
public HttpResponseMessage Post(MyPoco myPoco) { ... }
I've got the source code for aspnetwebstack and am trying to figure out if I can use a MediaTypeFormatter to accomplish this, but I'm not sure that I can. I understand somewhat model binding vs. media type formatting, that model binding leans towards the
query string and media type formatting leans towards the body.
What I'd like to do is hook into the action parameter binding directly and basically say "if this query string parameter exists, then use the query string like it was URL encoded form data media type and deserialize it into the parameter POCO, otherwise
use the normal model binding / media type formatting". EDIT 2012-11-02 11:22 EST: Query string would be formatted using jQuery $.param().
Is there any way to do this? Is there a way to register a MediaTypeFormatter to respond to "none", i.e. no content-type header in the request (which is what I get for JSON-P requests)?
I'm so close to getting this JSON-P support in transparently. I really don't want to hack my API and I don't want to create a custom version of Web API. So I'm hoping there's an appropriate extension point somewhere :) Help Obi-Wan, you're our only hope.
Oh wait, Donald Duck is available to help now, too...
I can't really help with your question, but I'm very interested in part "b" of your message and your custom IHttpActionSelector implementation. I have part "a" implemented and was just starting on part "b" when your message popped up. Any direction would
be greatly appreciated.
I have most of this accomplished via a) a custom MediaTypeFormatter that handles outputting of JSON/JSON-P depending on the presence of a query parameter, and
b) a custom IHttpActionSelector that routes to the Get, Post, etc. methods based on the value of another query parameter if it is present, otherwise default action selection.
Hi Mike,
Sorry I wasn't more clear, but I was talking about your two comments above. Thanks a ton for putting your IHttpActionSelector on GitHub. That should be a huge help.
MikeJansenRa...
Member
1 Points
6 Posts
Deserialize query string into action method POCO parameter without attributes
Nov 02, 2012 02:38 PM|LINK
(See this question on stackoverflow for additional background.)
I have a RESTful Web Service which supports CORS implemented with ASP.NET Web API. I am trying to transparently add JSON-P support without changing my controllers. I have most of this accomplished via a) a custom MediaTypeFormatter that handles outputting of JSON/JSON-P depending on the presence of a query parameter, and b) a custom IHttpActionSelector that routes to the Get, Post, etc. methods based on the value of another query parameter if it is present, otherwise default action selection.
The last piece that I have is being able to deserialize the query string into my action method POCO parameter without adding the [FromUrl] attribute, since I'd like my controllers to be just plain RESTful controllers and let the framework handle the difference between a regular/CORS request and a JSON-P request:
public HttpResponseMessage Post(MyPoco myPoco) { ... }I've got the source code for aspnetwebstack and am trying to figure out if I can use a MediaTypeFormatter to accomplish this, but I'm not sure that I can. I understand somewhat model binding vs. media type formatting, that model binding leans towards the query string and media type formatting leans towards the body.
What I'd like to do is hook into the action parameter binding directly and basically say "if this query string parameter exists, then use the query string like it was URL encoded form data media type and deserialize it into the parameter POCO, otherwise use the normal model binding / media type formatting". EDIT 2012-11-02 11:22 EST: Query string would be formatted using jQuery $.param().
Is there any way to do this? Is there a way to register a MediaTypeFormatter to respond to "none", i.e. no content-type header in the request (which is what I get for JSON-P requests)?
I'm so close to getting this JSON-P support in transparently. I really don't want to hack my API and I don't want to create a custom version of Web API. So I'm hoping there's an appropriate extension point somewhere :) Help Obi-Wan, you're our only hope. Oh wait, Donald Duck is available to help now, too...
Thanks,
Mike
jsonp
Davey528
Member
21 Points
6 Posts
Re: Deserialize query string into action method POCO parameter without attributes
Nov 02, 2012 08:19 PM|LINK
Hi Mike,
I can't really help with your question, but I'm very interested in part "b" of your message and your custom IHttpActionSelector implementation. I have part "a" implemented and was just starting on part "b" when your message popped up. Any direction would be greatly appreciated.
Thanks,
David
jsonp
MikeJansenRa...
Member
1 Points
6 Posts
Re: Deserialize query string into action method POCO parameter without attributes
Nov 05, 2012 12:41 PM|LINK
Hi David,
I tried to figure out what you meant by part a and part b but couldn't :)
I did put the source code by my IHttpActionSelector on github as a gist: https://gist.github.com/4017214.
Hope that helps!
Mike
jsonp
Davey528
Member
21 Points
6 Posts
Re: Deserialize query string into action method POCO parameter without attributes
Nov 05, 2012 01:14 PM|LINK
Hi Mike,
Sorry I wasn't more clear, but I was talking about your two comments above. Thanks a ton for putting your IHttpActionSelector on GitHub. That should be a huge help.
Regards,
David
MikeJansenRa...
Member
1 Points
6 Posts
Re: Deserialize query string into action method POCO parameter without attributes
Nov 05, 2012 05:51 PM|LINK
Ha, oops. Should have read my own post more clearly :O
Davey528
Member
21 Points
6 Posts
Re: Deserialize query string into action method POCO parameter without attributes
Feb 15, 2013 09:01 PM|LINK
Hi Mike,
Did you ever resolve your issue with serializing your query arguments into your POCO? Like you, I have everything working except that.
- David