views:

424

answers:

1

Can anyone recommend an article on sending and receiving JSON to an asp.net web service (any flavor) that uses more practical examples than "hello world".

Ideally, something that covers topics like:

Receive a single complex object from a web service (to display in a form)
Receive a collection of complex objects from a web service (to display in a table)
Send a single complex object to a web service (for updating the database)
Send a collection of complex objects to a web service (for updating the database)

+2  A: 

I have found this article to be useful in the past. It showcases much of what you are wanting to see. Hope this helps!

Edit: This question on SO has an excellent accepted answer showing the passing of complex data to a ASP.NET MVC controller method. Webservices work similarly in ASP.NET. They can accept an argument with a complex datatype populated with JSON from the client. You could replace the controller method with a similar WebMethod and return a class holding the desired return result:

[WebMethod]
public ReturnResult SaveWidget(Widget widget)
{
    // Save the Widget
    return new ReturnResult()
    { 
        Message = String.Format("Saved widget: '{0}' for ${1}", widget.Name, widget.Price) 
    };
}

With this class defined:

public class ReturnResult
{
    public string Message { get; set; }
}
Marve
nice article,but i remember having trouble enabling caching for the ajax request
zaladane
Thanks for that, looks like a good article...only receives data from the server though...anyone have a good example of sending modified data back to the server?
tbone
Thanks for the link to the other SO question...I dunno, that is MVC so I think performing the same in standard web services might be a bit different. However, on further examination of the first article you link, in the 2nd example, they are pulling data by passing in an integer to filter on....so, passing a complex object in should (lol) be a fairly simple variation on this. If I figure it out I'll try to come back and post the results.
tbone
You are welcome. Glad I could help. True there are differences between a controller method in ASP.NET MVC and a webservice method in standard ASP.NET, but if you code in the manner of my example, it will function similarly.
Marve
-1: Do not return object from a web service. Return a particular, named type. In particular, don't return an anonymous type. Doing so will force the client to parse XML instead of having a reasonable class to work with.
John Saunders
I wasn't aware of that. Thank you for the clarification. I updated my answer.
Marve
Thanks for the tip John!
tbone