客户端访问PageMethod
- 只能在aspx页面中定义
- 只能是public static方法
- 使用WebMethodAttribute标记
- ScriptManager的EnablePageMethod设置为true
- 通过pageMethods.MethodName进行访问
一个访问PageMethod的示例
创建一个页面,页面代码如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<input type="button" value="Get Current Time" onclick="getCurrentTime()" />
<script language="javascript" type="text/javascript">
function getCurrentTime() {
PageMethods.GetCurrentTime(getCurrentTimeSucceeded);
}
function getCurrentTimeSucceeded(result) {
alert(result);
}
</script>
</form>
</body>
</html>
后台代码中添加
[WebMethod]
public static DateTime GetCurrentTime()
{
return DateTime.Now;
}
主要,要引入using System.Web.Services命名空间
这样,我们就可以在点击按钮后访问PageMethod得到一个当前时间啦
错误处理
- 调用时,可以提供一个接收错误的回调函数
- 包括超时和服务器端抛出的异常
- 超时只能设置在WebService级别
- 由Sys.Net.WebServiceError提供
一个错误处理的示例
创建一个WebService添加如下代码
[WebMethod]
public int GetDivision(int a, int b)//这里我们会使用它抛出一个经典的除0错误
{
return a / b;
}
[WebMethod]
public int Timeout()//调用这个方法是,我们会首先设置它等待2秒,造成一个超时
{
System.Threading.Thread.Sleep(5000);
return 0;
}
然后创建一个页面,调用这个WebService
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Demo03/ErrorHandling.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" value="Get Division" onclick="getDivision(5,0)" />
<input type="button" value="TimeOut" onclick="timeout()" />
<script language="javascript" type="text/javascript">
function getDivision(a, b) {
ErrorHandling.GetDivision(a, b, null, failedCallback);
}
function timeout() {
ErrorHandling.set_timeout(2000);
ErrorHandling.Timeout(null, failedCallback);
}
function failedCallback(error) {
var message = String.format("Timeout:{0}\n Message:{1}\n Exception Type:{2}\n StackTrace:{3}", error.get_timedOut(), error.get_message(), error.get_exceptionType(), error.get_stackTrace());
alert(message);
}
</script>
这时,我们点击按钮,就可以看到一些错误信息了,实际应用中,我们可以利用这些信息,在页面上相应的做一些提示
复杂数据类型使用基础
首先,定义一个Employee类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///Employee 的摘要说明
/// </summary>
public class Employee
{
private string _firstname;
private string _lastname;
private string _title;
public Employee(string firstname, string lastname, string title)
{
this._firstname = firstname;
this._lastname = lastname;
this._title = title;
}
public Employee() { }
public string FirstName
{
get { return this._firstname; }
set { this._firstname = value; }
}
public string LastName
{
set { this._lastname = value; }
get { return this._lastname; }
}
public string Title
{
get { return this._title; }
}
public int Salary { get; set; }
public string FullName
{
get
{
return this.FirstName + this.LastName;
}
}
public static implicit operator string(Employee employee)
{
return employee.FullName;
}
}
然后创建一个WebService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
///ComplexType 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class ComplexType : System.Web.Services.WebService
{
[WebMethod]
public Employee DoubleSalary(Employee employee)
{
employee.Salary *= 2;
return employee;
}
[WebMethod]
public List<int> Reverse(List<int> list)
{
list.Reverse();
return list;
}
[WebMethod]
public IDictionary<string, Employee> GetEmployee()
{
Dictionary<string, Employee> result = new Dictionary<string, Employee>();
Employee emp1 = new Employee();
emp1.FirstName = "bai";
emp1.LastName = "yulong";
emp1.Salary = 2000;
result[emp1.FullName] = emp1;
Employee emp2 = new Employee();
emp2.FirstName = "xiao";
emp2.LastName = "yaojian";
emp2.Salary = 4000;
result[emp2.FullName] = emp2;
return result;
}
}
然后创建一个页面,使用他们,首先在页面中添加ScriptManager,引入上面创建的WebService,添加如下代码
<input type="button" value="Double Salary" onclick="doubleSalary()" />
<input type="button" value="Reverse" onclick="reerse([1,2,3,4,5])" />
<input type="button" value="Get Employee" onclick="getEmploee()" />
<script language="javascript" type="text/javascript">
function doubleSalary() {
var employee = new Object();
employee.FirstName = "bai";
employee.LastName = "yulong";
employee.Salary = 1000;
ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
}
function doubleSalarySucceeded(result) {
var message = String.format("FirstName:{0}\nLastName:{1} \nFullName:{2} \nSalary:{3}", result.FirstName, result.LastName, result.FullName, result.Salary);
alert(message);
}
function reerse(array) {
ComplexType.Reverse(array, function(result) { alert(result); });
}
function getEmploee() {
ComplexType.GetEmployee(getEmployeeSucceeded);
}
function getEmployeeSucceeded(result) {
for (var name in result) {
alert(name + ":" + result[name].Salary);
}
}
</script>
这时,我们点击"Double Salary"按钮,就可以调用WebService上的DoubleSalary方法,使工资翻倍啦
如果我们这时用HTTP Watch看的话,就可以看见我们发送的是一个JSON字符串,返回的同样是一个JSON字符串,但是他在前面使用__type指定了一个Employee类型
其他的两个方法,演示的就是实现了IList和IDictionary接口的类型的使用方式,这里使用一些工具,就可以很明显的看到他们在发送和接受数据中的方式