views:

72

answers:

2

hi, I have general question that if an asp.net webmethod always returns data in 'json' form.If not what is the default return datatype of a webmethod?

I am wondering if there is a way I can get the data from a webmethod in 'HTML' rather than 'json'?

Thanks

A: 

The return type for a default webservice should SOAP (an XML Format), for WCF there is a REST pack which can change it return type to JSON

[http://msdn.microsoft.com/en-us/netframework/cc950529.aspx][1]

Hope this helps.

Iain
Thanks.actually I am not using webservices.but I am writing a webmethod in my asp.net codebehind page and I always see the response in json.Could you please explain why? and how to get 'HTML' instead of 'json'?
kranthi
could you a code sample?you may also want to add a content type on to the page directive
Iain
for example I have the following method [WebMethod] public static string sayHello() { return "hello"; } and I am calling this method using scriptmanager and PageMethods.sayHello combination and in firebug I see the response as {"d":"hello"}.I have also tried setting up the page directive ContentType to 'text/html' and still got the same json response.
kranthi
@kranthi - check my answer above. FYI - PageMethods are still web services, they are just called that to give the feel of calling a 'method in the page' in your javascript/ajax, when in fact you are executing an ASP.NET AJAX-enabled web service (on the server).
RPM1984
A: 

PageMethods are a part of the ASP.NET AJAX Framework (ScriptManager).

By default, ASP.NET AJAX uses JSON as opposed to SOAP.

This is by design, mainly because the ASP.NET AJAX Javascript library it optimized to work with JSON objects.

You can override this default by specifying the [ResponseFormat] attribute on the web method.

Like this:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlElement GetFoo(string url)
{

Here is a decent article on PageMethods and ASP.NET AJAX-enabled web services.

RPM1984
Sorry for the delay in reply and thank you so much for referring me to this article.As per my understanding a page method will either give a xml or a Json response and not anything else.Please correct me if I am wrong?
kranthi
Your correct. I always use JSON when consuming internally, because most of the time i will be using the web service in jquery/ajax. The only time i'll use XML is in addition to JSON when creating an external API/web service.
RPM1984