通用的ashx调用
直接上代码
还是有一定通用性的
<%@ 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调用的更多相关文章
- Winform开发框架之通用Windows摄像头调用拍照--SNF快速开发平台3.3-Spring.Net.Framework
今天做了一个windows系统下调用摄像头.进行开启.关闭.拍照.设置等等功能演示. 进行源码贡献,欢迎大家下载使用 一.DEMO效果如下: 二.DEMO演示代码如下: using SNF.Utili ...
- ashx调用session对象
1.引入命名空间 using System.Web.SessionState 2.必须实现接口 public class Login : IHttpHandler, IRequiresSessionS ...
- orm 通用方法——RunProc调用存储过程
该方法暂不支持带返回值的存储过程,期待能人补充指点. 定义代码: /** * 描述:执行存储过程 * 作者:Tianqi * 日期:2014-09-16 * param:rs orm.RawSeter ...
- 如何优雅的使用Fegin去构造通用的服务调用的API
第一步: 创建一个公共的API服务:命名为api(根据自己实际情况进行命名) <?xml version="1.0" encoding="UTF-8"?& ...
- SNF开发平台WinForm之十二-发送手机短信功能调用-金笛-SNF快速开发平台3.3-Spring.Net.Framework
1.调用前组装参数 2.调用发送信息服务脚本 .调用前组装参数: BaseSendTaskEntity entity = new BaseSendTaskEntity(); entity.Mess ...
- ASP.net与SQLite数据库通过js和ashx交互(连接和操作)
ASP.net与SQLite数据库通过js和ashx交互(连接和操作): 废话(也是思路):用的是VS2010,打算做网站前后台.由于不喜欢前台语言里加些与html和css和js的其他内容,想实现前后 ...
- 通用mapper认识和用法
目录 0. 认识 1. 导包 2. mybatis的config文件:mybatis-mapper-config.xml 3. spring与mybatis整合配置文件:mybatis.xml 4. ...
- SNF开发平台WinForm-表单验证控件-通用
CS程序也能做到像BS程序一样的验证效果,如下: 1.验证控件的展示 校验时如果不符合验证条件的控件,会在控件上显示较显眼的图标. 当出现不符合验证的控件时,鼠标悬浮会显示自定义的提示信息. 如:输入 ...
- SNF快速开发平台MVC-EasyUI3.9之-WebApi和MVC-controller层接收的json字符串的取值方法和调用后台服务方法
最近项目组很多人问我,从前台页面传到后台controller控制层或者WebApi 时如何取值和运算操作. 今天就都大家一个在框架内一个取值技巧 前台JS调用代码: 1.下面是选中一行数据后右键点击时 ...
随机推荐
- BZOJ_2111_[ZJOI2010]Perm 排列计数_树形DP+组合数学
Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...
- bzoj 2865 字符串识别 —— 后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2865 唯一出现的子串就是每个后缀除去和别的后缀最长的 LCP 之外的前缀: 所以用这个更新一 ...
- C# 获取Console的输入和输出 数据 (异步)
using System ; using System .Diagnostics; using System .IO; class Program { static void Main() ...
- WPF ListView VisualPanel
<ItemsPanelTemplate x:Key="ItemsPanelTemplate1"> &l ...
- windows下VisualStudio和QtCreator搭建Qt开发环境
一.简介 集成开发平台IDE都有各自的长处,编写Qt程序可根据自己的喜好来选择相应的IDE.下述文章都是装载博友的文章,其中有很多细节还得自己调整. 二.详解 1.VisualStudio搭建Qt开发 ...
- ElementRef, @ViewChild & Renderer
ElementRef: In Angular2 Doc, it suggest to "avoid" using ElementRef. It access DOM directl ...
- JMeter聚合报告(Aggregate Report)理解
部分内容转载: http://blog.csdn.net/lion19930924/article/details/51189218 http://www.cnblogs.com/fnng/archi ...
- WCF部署到IIS上调用报错:由于扩展配置问题而无法提供您请求的页面
将WCF部署到全新win7 x64 IIS7.5上访问报错:由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 原因:IIS不识别.sv ...
- The Truth About GCHandles
I've heard several people asking why GCHandle doesn't implement IDisposable, considering it wraps an ...
- HDU 1556 Color the ball 前缀和+思维
Color the ball N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球 ...