Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Web API Deserialize Query Parameters into Nested POCO Action Parameter
    text
    copied!<p>Already checked <a href="https://stackoverflow.com/questions/9802170/webapi-deserialize-uri-parameter-to-poco-action-parameter">this question</a> but didn't answer.</p> <p><strong>Background</strong></p> <p>I have a fully functional RESTful web service written using ASP.NET Web API and it currently supports <a href="http://en.wikipedia.org/wiki/Cross-origin_resource_sharing" rel="nofollow noreferrer">CORS</a> effectively for cross-origin access from browsers that support CORS. Problem is that current business needs require support of browsers that don't support CORS. I am adding <a href="http://en.wikipedia.org/wiki/JSONP" rel="nofollow noreferrer">JSON-P</a> support to my web service in addition to supporting CORS and through the magic of action selectors and type formatters, my actual web service code hasn't changed....yet.</p> <p>Currently I use nested POCO objects (objects that contain other objects) as parameters, for example, for my Post actions. Since I'm supporting XML and JSON incoming, the POST data gets deserialized nicely into the POCO objects, since both XML and JSON support nested deserialization. But to support JSON-P, I have to now emulate a POST through Query Parameters. Getting to the Post action method is successful via an httpMethod Query Parameter and a custom action selector.</p> <p><strong>Question(s)</strong></p> <p>First of all, and I ask this after reading responses to other questions, will the registered type formatters even access the Query Parameters for deserializing if I have no request body? The JSON-P request is going to be a simple GET request with no body, so I'm not even sure if it is possible to have a POCO in my action parameter and have it deserialized with a GET request and only Query Parameters.</p> <p><strong>EDIT</strong>: Looks like I may be able to do some MediaTypeFormatter magic with a custom formatter and using the QueryStringMapping. Not sure yet though.</p> <p>Second, is it possible to deserialize Query Parameters into nested properties of the POCO object? And if so, what is the naming convention for Query Parameters to do this? For example XML of Bob would get deserialized into Message.User.FirstName if the action parameter was of type Message.</p> <p><strong>EDIT</strong>: <code>FormUrlEncodedMediaTypeFormatter</code> has some of the functionality that I want if I could just redirect it to use the Query String instead of the body. But I also don't want a <code>JToken</code> object -- I'd like my POCO, but I think I can deserialize a <code>JToken</code> with JSON.NET. So I'm probably going to steal the code from <code>FormUrlEncodedMediaTypeFormatter</code> and the relate internal class <code>FormUrlEncodedJson</code> to make a custom formatter. Just need to make sure question #1 is possible first.</p> <p><strong><em>Example POCOs</em></strong></p> <pre><code>public class User { public string FirstName { get; set;} } public class Message { public User User { get; set; } } </code></pre> <p><strong><em>Example "standard" RESTful POST</em></strong></p> <pre><code>POST /api/messages Content-Type: text/xml &lt;Message&gt;&lt;User&gt;&lt;FirstName&gt;Bob&lt;/FirstName&gt;&lt;/User&gt;&lt;/Message&gt; </code></pre> <p><strong><em>Example Hypothetical JSON-P Simulated POST</em></strong></p> <pre><code>&lt;script type="text/javascript" src="https://api.mydomain.com/api/messages?callback=MyCallback&amp;httpMethod=Post&amp;User.FirstName=Bob"&gt; &lt;/script&gt; </code></pre> <p><strong>EDIT: Overall Goal:</strong> I'm trying to leave the action methods alone right now if possible since they currently handle RESTful CORS-enabled requests. My goal is to add JSON-P support to the calls without changing the method signature. I've got that mostly accomplished; all I have left is being able to deserialize the Query Parameters into the action method parameters when it's a JSON-P request.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload