# IT:AD:ASP.NET:Win Forms:AJAX # * [[../|(UP)]] {{indexmenu>.#2|nsort tsort}} ## Attributes ## ### Attributes:ASP.NET ### There are a couple of Attributes used to make methods accessible from script: * `WebMethodAttribute` * Ref: [MSDN](http://bit.ly/Ly05W1). * To make them invocable from Script, *MUST* be added to: * *WebService* methods, * *ASPX* static callback methods. * Without `WebMethod` applied, not sure if it means it can only be called from within same network, or not at all. * `ScriptMethodAttribute` * Ref: [MSDN](http://bit.ly/KRUKG4) * *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 with `ScriptMethodAttribute`, will require the request to come in as a `POST`, not a `GET`. * As `$.ajax` uses `GET`, consider `$.post` when working with ASP.NET * For Microsoft AJAX to work in ASP.NET pages, `ScriptMethod` is 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](http://bit.ly/KSbA7G): * Used to decorate the method so that it varies response type (JSON|XML) based on ContentType. * Ref: [Post](http://bit.ly/KThSEc) In .NET40, enable `automaticFormatSelectionEnabled` in config using either standardEndpoint or endpoint behaviours. See this [page](http://www.biztalkgurus.com/biztalk_server/biztalk_blogs/b/biztalksyn/archive/2010/02/01/returning-custom-formats-from-wcf-webhttp-services.aspx) and [this msdn page](http://msdn.microsoft.com/en-us/library/ee476510.aspx). ### Attributes:WebServices ### * `WebMethodAttribute`: * Ref: [MSDN](http://bit.ly/Ly05W1). * To make them invocable from Script, *MUST* be added to: * *WebService* methods, * *ASPX* static callback methods. * Without `WebMethod` applied, not sure if it means it can only be called from within same network, or not at all. * `ScriptMethodAttribute`: * Ref: [MSDN](http://bit.ly/KRUKG4). * *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 with `ScriptMethodAttribute`, will require the request to come in as a `POST`, not a `GET`. * As `$.ajax` uses `GET`, consider `$.post` when 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](http://bit.ly/LyCi8v) * 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](http://bit.ly/KSMKVm) * 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); } ## RAW ## [http://andre-pedroso.blogspot.co.nz/2011/02/post-json-to-mvc-controller-with.html](http://andre-pedroso.blogspot.co.nz/2011/02/post-json-to-mvc-controller-with.html) ## Resources ##