通用的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.下面是选中一行数据后右键点击时 ...
随机推荐
- ACM学习历程—HDU 2795 Billboard(线段树)
Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...
- 【Lintcode】088.Lowest Common Ancestor
题目: Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two n ...
- bzoj 4883 [Lydsy1705月赛]棋盘上的守卫——并查集(思路!)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4883 把各行和各列看成n+m个点. 如果一下能防守行和列,就是最大匹配了.这是每两个左右部点 ...
- wpf如何获取control template里的元素
wpf中的控件模板里的元素有自己独立的命名域. 因此不能通过FindName来根据x:Name来查找子节点. 自己写了一个方法, 通过可视树遍历子节点,然后匹配名字. 如下: private stat ...
- Mysql 创建存储过程 更新表
DELIMITER // use protocoldb// drop procedure if exists sp_protocol_Update// create procedure sp_prot ...
- TextBox的OnTextboxChanged事件里对Text重新赋值带中文, 导致崩溃
今天遇到一个超级bug, Textbox做了限制, 只能输入数字. 结果在搜狗输入法输入中文时导致崩溃, 出错信息如下: 未处理 System.InvalidOperationException ...
- IOS推流 搭建环境
效果图 iTools有点卡, 但是推到服务器倒是很快的. 推流 前言 这篇blog是iOS视频直播初窥:<喵播APP>的一个补充. 因为之前传到github上的项目中没有集成视频的推流.有 ...
- 反射设置当前窗体所有控件的Text
在我们编程的时候,有时需要动态的获取当前窗体控件的Text,但是又不能一个一个控件的设置,这个时候可以通过反射来动态设置. 第一步:先建立一个类来保存控件的Text信息. public class C ...
- java之数学方法
参考http://how2j.cn/k/number-string/number-string-math/319.html java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法 ...
- WP之样式
1.定义资源 <Window.Resources> <!--下面用样式--> <Style x:Key="BigFontButtonStyle"> ...