通用的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.下面是选中一行数据后右键点击时 ...
随机推荐
- 「LuoguP1429」 平面最近点对(加强版)
题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 接下来n行:每行两个实数:x y, ...
- poj 3415 Common Substrings —— 后缀数组+单调栈
题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...
- noip2013提高组:积木大赛
题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第n块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...
- 开源LTE代码分析
跟踪了一个在将开源组织-OpenLTE(将4G通信网络LTE开源),现将自己梳理整理的一些文档Post出来,请有相同兴趣的朋友指点: 一,系统介绍 OpenLTE是一位Mot的工程师在12年发起的一个 ...
- Linux 包管理基础:apt、yum、dnf 和 dpkg
https://linux.cn/article-8782-1.html 1. apt-get 安装( 在线) 会帮我把所有的依赖包都一起安装 apt-get install xxx 安装xxx .如 ...
- vmware中centos6.5无法启动拷贝出里面的资料的方法
先说一下我的环境:windows7-x64位机器下安装的vmware虚拟机,里面安装的是centos6.5-x64位的系统. 系统崩溃的原因:从cenos拖拽一个文件到win7下,结果就卡死了.整个系 ...
- ACM-ICPC2018徐州网络赛 Hard to prepare(dp)
Hard to prepare 28.63% 1000ms 262144K After Incident, a feast is usually held in Hakurei Shrine. T ...
- debian 7上源码编译MongoDB 3.4版本
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 要想精通一个数据库,除了知道该数据库的功能特性.使用方法等,还需要能够看懂数据库源码,能够使用gdb工具对其 ...
- 成为高手前必懂的TCP干货
目录 一.起源 二.TCP 协议 TCP 的特点? 怎么理解全双工? TCP 的数据包如何组织? 三.TCP 工作流程 四. 三次握手 五. 四次挥手 小结 我们在平时的开发过程中,或多或少都会涉猎到 ...
- lower_bound和upper_bound使用说明
#include <bits/stdc++.h> using namespace std; int main() { ]; ;i<=;i++) { a[i] = i*; } ;i&l ...