IT:AD:ASP.NET:Win Forms:AJAX

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 WebMethod applied, 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 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:
      • Used to decorate the method so that it varies response type (JSON|XML) based on ContentType.
      • Ref: Post

      In .NET40, enable automaticFormatSelectionEnabled in config using either standardEndpoint or endpoint behaviours. See this page and this msdn page.

  • WebMethodAttribute:
    • Ref: MSDN.
    • 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.
    • 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.

  • 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);
    }

  • /home/skysigal/public_html/data/pages/it/ad/asp.net/win_forms/ajax.txt
  • Last modified: 2023/11/04 03:37
  • by 127.0.0.1