Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebMethod vs ScriptMethod

People also ask

What is Scriptmethod?

This attribute is used to specify information for methods that can be called from client script. The attribute lets you specify which HTTP verb (GET or POST) can be used to invoke a method. It also lets you specify whether the response is formatted by using either JavaScript Object Notation (JSON) or XML.

Why we use WebMethod?

webMethods.io Integration enables you to automate tasks by connecting apps and services, such as Marketo, Salesforce, Evernote, and Gmail. It lets your favorite apps exchange data and talk to each other seamlessly, and eliminates the need to hire expensive developers to build your favorite integrations.

What is WebMethod in Web service?

The WebMethod attribute can make development of XML Web Services easier. WebMethods make the development of XML Web Services easier by encapsulating a good deal of functionality. Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service.

What is WebMethod attribute?

This attribute is used to customize the behavior of your Web Service. This attribute holds up several properties like enable session, description, cacheduration, and bufferresponse. I discussed the enable session property in my previous article.


You use the ScriptMethod attribute in the following 2 scenarios.

  1. You are using jquery or any other ajax request mechanism, but you want the request to be a GET not a POST.
  2. You want to receive an XML formated response in javaScript.

If you don't have one of the above requirements; you just need a JSON response using a an ajax request then you can simply use the WebMethod.

There is still one more confusing element here, when do you use the ScriptService attribute? this is used if you are using the Microsoft Ajax Client script framework, this attributes tell the server to generate proxy objects on the client so that you can call functions just like a normal object. var MyRemoteObject = new RemoteObject(); MyRemoteObject.getMessage(....) and even when you use the ScriptService attribute you don't need to add the ScriptMethod attribute only in the above scenarios.

It was confusing to me at the begining because i thought that the ScriptService and the ScriptMethod attributes works together just like the WebService and WebMethod attributes.


The ScriptMethodAttribute attribute is optional. (However, methods that can be called from client script must have the System.Web.Services..::.WebMethodAttribute attribute applied.). If a method is not marked with ScriptMethodAttribute, the method will be called by using the HTTP POST command and the response will be serialized as JSON. You cannot override this setting from script.

from - http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx

EDIT: WebMethod and ScriptMethod are not competing attributes. ScriptMethod could be an additional annotation, as the above para says.