直接上代码
还是有一定通用性的
<%@ WebHandler Language="C#" Class="MyService" %> using System;
using System.Web;
using System.Collections.Generic;
/// <summary>
/// 测试访问路径:http://localhost:2484/TestAjaxFrameWork/MyService.ashx?c=class1&m=hello&parm1=23&parm2=33&parm3=4
/// </summary>
public class MyService : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
AjaxHelper ajax = new AjaxHelper(context.Request.QueryString["c"], context.Request.QueryString["m"]);
List<string> objlist = new List<string>();
foreach (string item in context.Request.QueryString.Keys)
{
if (item != "c" && item != "m")
{
objlist.Add(context.Request.QueryString[item]);
}
}
object[] parms = objlist.ToArray();
ajax.ProcessRequest(context, parms);
} public bool IsReusable
{
get
{
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Web.Compilation;
using System.Web; /// <summary>
/// AjaxHelper 的摘要说明
/// </summary>
public class AjaxHelper
{
/// <summary>
/// 要访问的类名
/// </summary>
public string ClassName { get; set; } /// <summary>
/// 要访问的方法
/// </summary>
public string MethodName { get; set; } /// <summary>
/// 构造函数初始化类名,方法
/// </summary>
/// <param name="className"></param>
/// <param name="methodName"></param>
public AjaxHelper(string className, string methodName)
{
this.ClassName = className;
this.MethodName = methodName;
} public void ProcessRequest(HttpContext context, object[] parms)
{
//类名方法名不能为空啊
if (string.IsNullOrEmpty(this.ClassName) || string.IsNullOrEmpty(this.MethodName))
{
Report404Error(context);
return;
} //获取当前程序集,这就限定了,调用的ajax类必须和当前类是一个程序集了
Assembly curAssembly = Assembly.GetExecutingAssembly(); //取得包含ajax类特性的公共类
var ts = from t in curAssembly.GetExportedTypes()
let a = (AjaxClassAttribute[])t.GetCustomAttributes(typeof(AjaxClassAttribute), false)
where a.Length > 0
select t; //获取当前访问的类型
Type type = ts.FirstOrDefault(t => string.Compare(this.ClassName, t.Name, true) == 0); //获取当前访问的方法
MethodInfo method = type.GetMethod(this.MethodName,
BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); //检测是否包含ajax方法特性
AjaxMethodAttribute[] attrs = (AjaxMethodAttribute[])
method.GetCustomAttributes(typeof(AjaxMethodAttribute), false);
if (attrs.Length != 1)
{
Report404Error(context);
}
//调用方法奥
method.Invoke(Activator.CreateInstance(type), parms);
} private void Report404Error(HttpContext context)
{
throw new HttpException(404,
"没有找到能处理请求的服务类,当前请求地址:" + context.Request.RawUrl);
}
}
/// <summary>
/// 自定义特性,表示ajax类
/// </summary>
public class AjaxClassAttribute : Attribute
{ } /// <summary>
/// 自定义特性,表示ajax方法奥
/// </summary>
public class AjaxMethodAttribute : Attribute
{ }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; [AjaxClass]
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
} [AjaxMethod]
public void hello(string parm1, string parm2, string parm3)
{
HttpContext.Current.Response.Write("test context," + parm1 + "," + parm2 + "," + parm3 + ",");
}
}