Feedback on ASP.NET Web API

Please provide some sample to learn how we can pass complex object to web api from C# code.

I am not able to find any sample where i could learn how can i pass complex object to web api. suppose there is an object 'user' with its username, emailid, and roles. now roles is a list of another object role. a user can have multiple roles. my api is not firing when i pass user object. how can we do that ?

1 vote
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Priyank Saxena shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
completed  ·  Daniel Roth responded  · 

Sure, we have several samples that show how to do this. You can take a look at the Contact Manager sample (http://code.msdn.microsoft.com/Contact-Manager-Web-API-0e8e373d). Or alternatively, install the latest preview of the ASP.NET and Web Tools 2012.2 release and take a look at the web APIs in the new MVC SPA template.

Basically all you need to do is add a parameter to your action method of the type you defined. The action method should probably be for POST requests (which is the default, or just prefix your method name with “Post”).

If your method is not firing like you expect then you need to make sure you are sending the right request. The default Web API route is api/{controller}/{id} where the id is optional. So if you have FooController you would send the request to api/foo. The FooController should define the action method that handles the POST request. Something like PostFoo(Foo foo).

The trick then is to send the right content in the request so that the data deserializes correctly. A simple way to do this is to install the Web API Help Page preview feature (http://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage/0.3.0-rc) and then look at the sample request formatting.

Hope this helps!

1 comment

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base