直接上代码

还是有一定通用性的

<%@ 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 + ",");
}
}

  

通用的ashx调用的更多相关文章

  1. Winform开发框架之通用Windows摄像头调用拍照--SNF快速开发平台3.3-Spring.Net.Framework

    今天做了一个windows系统下调用摄像头.进行开启.关闭.拍照.设置等等功能演示. 进行源码贡献,欢迎大家下载使用 一.DEMO效果如下: 二.DEMO演示代码如下: using SNF.Utili ...

  2. ashx调用session对象

    1.引入命名空间 using System.Web.SessionState 2.必须实现接口 public class Login : IHttpHandler, IRequiresSessionS ...

  3. orm 通用方法——RunProc调用存储过程

    该方法暂不支持带返回值的存储过程,期待能人补充指点. 定义代码: /** * 描述:执行存储过程 * 作者:Tianqi * 日期:2014-09-16 * param:rs orm.RawSeter ...

  4. 如何优雅的使用Fegin去构造通用的服务调用的API

    第一步: 创建一个公共的API服务:命名为api(根据自己实际情况进行命名) <?xml version="1.0" encoding="UTF-8"?& ...

  5. SNF开发平台WinForm之十二-发送手机短信功能调用-金笛-SNF快速开发平台3.3-Spring.Net.Framework

    1.调用前组装参数 2.调用发送信息服务脚本   .调用前组装参数: BaseSendTaskEntity entity = new BaseSendTaskEntity(); entity.Mess ...

  6. ASP.net与SQLite数据库通过js和ashx交互(连接和操作)

    ASP.net与SQLite数据库通过js和ashx交互(连接和操作): 废话(也是思路):用的是VS2010,打算做网站前后台.由于不喜欢前台语言里加些与html和css和js的其他内容,想实现前后 ...

  7. 通用mapper认识和用法

    目录 0. 认识 1. 导包 2. mybatis的config文件:mybatis-mapper-config.xml 3. spring与mybatis整合配置文件:mybatis.xml 4. ...

  8. SNF开发平台WinForm-表单验证控件-通用

    CS程序也能做到像BS程序一样的验证效果,如下: 1.验证控件的展示 校验时如果不符合验证条件的控件,会在控件上显示较显眼的图标. 当出现不符合验证的控件时,鼠标悬浮会显示自定义的提示信息. 如:输入 ...

  9. SNF快速开发平台MVC-EasyUI3.9之-WebApi和MVC-controller层接收的json字符串的取值方法和调用后台服务方法

    最近项目组很多人问我,从前台页面传到后台controller控制层或者WebApi 时如何取值和运算操作. 今天就都大家一个在框架内一个取值技巧 前台JS调用代码: 1.下面是选中一行数据后右键点击时 ...

随机推荐

  1. vim 模式下的几个快捷用法

    1.ctrl + v  (-- VISUAL BLOCK --) 选中块模式,y 复制,d 剪切,p 粘贴,Esc退出模式 2.Shift + v  (-- VISUAL LINE -- ) 快速行选 ...

  2. [转载]IOCP模型的总结

    原文:IOCP模型的总结 IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型.它是应用程序使用线程池处理异步I/O请求的一种机制.在处理多个并发的异步I/O请 ...

  3. BZOJ4571:[SCOI2016]美味

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  4. MongoDB主从复制,主主复制

    MongoDB主从复制,是不需要像mysql那样从数据库事先要完整的主数据快照背景介绍:mongodb支持一主一从或多从复制1)    master节点: mongod --dbpath=/usr/M ...

  5. 虚拟机VMware Workstation cannot connect to the virtual machine

    解决方法: 从提示消息我们可以看到,问题在于VMware授权服务没有开启,具体处理方法如下: "This PC(我的电脑)"---右键"manage(管理)"- ...

  6. vmware ubuntu14.04 忘记密码

    重置root密码 启动系统,一直点击esc键盘,出现如下界面,选择Advanced options for Ubuntu 按回车键确认: 选择recovery mode,按 e : 到 linux / ...

  7. lwip【4】 lwIP配置文件opt.h和lwipopts.h初步分析之一

    在这里先说一下这两个配置lwip协议栈文件opt.h和lwipopts.h的关系:          opt.h是lwip"出厂"时原装的配置文件,它的作者是瑞士科学院的Adam等 ...

  8. HDU-5979

    Convex Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  9. linux多线程加解锁

    1.动态方式使用互斥量,该类型的互斥量在定义时不进行初始化,需要在使用之前初始化,使用结束销毁 1.1.定义一个锁变量:         pthread_mutex_t g_mutex_Msg; 1. ...

  10. ACM-ICPC2018焦作网络赛 Participate in E-sports(大数开方)

    Participate in E-sports 11.44% 1000ms 65536K   Jessie and Justin want to participate in e-sports. E- ...