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:
- The method MUST be static
- The method needs to be decorated with [WebMethod()]
- 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.
精彩评论