开发者

Including webservice function in aspx file

I read an article some time back that explaind how I could add a webservice function to my aspx file for my ajax callbacks to call. Now I can't find this article or any other 开发者_如何学运维documentation on this.

Anyone using this and can you explain how to do this?

Thanks

Endre


I believe you need to mark the method as a [WebMethod]

http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx

From the above article:

  1. The method MUST be static
  2. The method needs to be decorated with [WebMethod()]
  3. The method needs to be decorated with [ScriptMethod()] if you want to make a ASP.NET AJAX callback to it

public partial class Products : System.Web.UI.Page

{ [System.Web.Services.WebMethod()] [System.Web.Script.Services.ScriptMethod()]

public static List GetProducts(int cateogryID) {

// Put your logic here to get the Product list }


You can call pretty much any method that has the attribute [WebMethod]

Perhaps this was the article you read?


I'd bear in mind that it's often best to keep your web services seperate from the pages that call them.

To create a simple web service in an aspx file, you'd use something like this:

<%@ WebService Language="C#" Class="MyWebService" %>

using System;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://www.example.com/webservices/MyWebService, Description = "My Web Service")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyWebService : WebService
{

    [WebMethod(Description = "Add two numbers and return the result.")]
public int AddNumbers(int first, int second) {
        return first + second;
}

}

If you are looking for solid cross platform dynamic JavaScript component which can talk to your web service I'd check out http://www.guru4.net/articoli/javascript-soap-client/en/ (I use this and highly recommend it).

Alternatively, you could use something like jQuery to access the REST interface, or parse the XML from the SOAP response yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜