问题
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)
回答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; }
}
来源:https://stackoverflow.com/questions/1301802/examples-of-practical-usage-of-json-to-and-from-an-asmx-web-service-via-jquery