0

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)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
tbone
  • 5,715
  • 20
  • 87
  • 134

1 Answers1

1

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; }
}
Community
  • 1
  • 1
dcharles
  • 4,822
  • 2
  • 32
  • 29
  • nice article,but i remember having trouble enabling caching for the ajax request – ak3nat0n Aug 19 '09 at 18:41
  • 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 Aug 19 '09 at 19:10
  • 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 Aug 19 '09 at 20:54
  • 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. – dcharles Aug 19 '09 at 21:00
  • -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 Aug 19 '09 at 22:16
  • For older ASMX services, you can only post simple variable types (ie string, int) to an ASMX webmethod. Then parse your string (json) and map it manually in code to a C# object. Best to upgrade all your web services (to MVC), then this code example will work as written. ASMX can't handle objects as parameters. – MC9000 Jul 12 '22 at 16:52