IT:AD:ASP.NET:Win Forms:AJAX
Attributes
Attributes:ASP.NET
There are a couple of Attributes used to make methods accessible from script:
WebMethodAttribute- Ref: MSDN.
- To make them invocable from Script, MUST be added to:
- WebService methods,
- ASPX static callback methods.
- Without
WebMethodapplied, not sure if it means it can only be called from within same network, or not at all.
*
ScriptMethodAttribute- Ref: MSDN
- Optional: used to decorate methods intended to be called by Script.
- Specifies which HTTP verb is used to invoke a method (GET|POST), and the format of the response (JSON|XML) (Default is POST|JSON).
- Important
WebService Methods, unless they are decorated withScriptMethodAttribute, will require the request to come in as aPOST, not aGET. - As
$.ajaxusesGET, consider$.postwhen working with ASP.NET - For Microsoft AJAX to work in ASP.NET pages,
ScriptMethodis required.- Partly due to the fact that it uses GET to invoke the static callbacks.
- Partly (although I'm a bit fuzzy here) it adds the d value when serializing.
*
AutomaticFormatSelectionEnabled:- Ref: MSDN:
- Used to decorate the method so that it varies response type (JSON|XML) based on ContentType.
- Ref: Post
In .NET40, enable
automaticFormatSelectionEnabledin config using either standardEndpoint or endpoint behaviours. See this page and this msdn page.
Attributes:WebServices
WebMethodAttribute:- Ref: MSDN.
- To make them invocable from Script, MUST be added to:
- WebService methods,
- ASPX static callback methods.
- Without
WebMethodapplied, not sure if it means it can only be called from within same network, or not at all.
*
ScriptMethodAttribute:- Ref: MSDN.
- Optional: used to decorate methods intended to be called by Script.
- Specifies which HTTP verb is used to invoke a method (GET|POST), and the format of the response (JSON|XML) (default is POST|JSON).
- Important
WebService Methods, unless they are decorated withScriptMethodAttribute, will require the request to come in as aPOST, not aGET. - As
$.ajaxusesGET, consider$.postwhen working with ASP.NET
- Note that callback method will serialize its results in POX, JSON, or string depending on how it's called.
Personally, I don't do WebServices. Period. Pre-SOAP protocol that is depracated.. Use WCF and WebAPI, with WebHttpBehavior, etc.
Attributes:WCF
WebGetAttribute- Ref: MSDN
- Used in WebServices to make them accessible in a RESTful manner.
- A Passive Attribute (adds no functionality) except if used by
WebHttpBehavior. - Associates the Operation with the GET statement (not PUT). See security issue above.
- Prefer WebInvoke to WebGet (see below).
- Requires WebHttpBehavior to be applied to webservice.
Examples:
[ServiceContract]
public interface ICalculator
{
[OperationContract]
[WebGet(UriTemplate = "Mult?x={x}&y={y}", BodyStyle = WebMessageBodyStyle.Bare)]
long Multiply(long x, long y);
[OperationContract]
[WebGet(UriTemplate = "Div?x={x}&y={y}", RequestFormat = WebMessageFormat.Xml)]
long Divide(long x, long y);
[OperationContract]
[WebGet(ResponseFormat= WebMessageFormat.Json)]
long Mod(long x, long y);
}
WebInvoke- Ref: MSDN
- Defined in:
System.ServiceModel.Web - By Default associates the Operation to POST. If you want GET, use WebGet
- Prefer WebInvoke to WebGet
- Requires WebHttpBehavior to be applied to webservice.
- UriTemplate is how you to transform Svc to being RESTful (see IT:AD:WCF/REST)
Example:
[ServiceContract]
public interface ICalculator2
{
[OperationContract]
[WebInvoke]
long Add(long x, long y);
[OperationContract]
[WebInvoke(UriTemplate = "Sub?x={x}&y={y}")]
long Subtract(long x, long y);
[OperationContract]
[WebInvoke(UriTemplate = "Mult?x={x}&y={y}", BodyStyle = WebMessageBodyStyle.Bare)]
long Multiply(long x, long y);
[OperationContract]
[WebInvoke(UriTemplate = "Div?x={x}&y={y}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml)]
long Divide(long x, long y);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Mod?x={x}&y={y}")]
long Mod(long x, long y);
}