asp.net 使用JQuery 调用Ashx 后面直接写方法名,通过反射找到对应的方法
using System.Reflection;
public class Industry_Manager : IHttpHandler
{
HttpRequest gRequest = null;
HttpContext gContext = null;
HttpResponse gResponse = null;
string func = string.Empty;
string result = string.Empty;
string pageUrl = string.Empty; public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
gContext = context;
gRequest = context.Request;
gResponse = context.Response;
func = gRequest["func"];
MethodInfo method = typeof(Industry_Manager).GetMethod(func);
if (method != null)
{
object[] args = new object[] { result };
method.Invoke(this, args);
result = (string)args[0];
}
gResponse.Write(result);
}

js 代码
url: "http://www.cnblogs.com/Ashx/Industry_Manager.ashx?func=GetIndustryList", //请求数据的页面,后面参数直接跟方法名就可以了,后台通过反射自动查找,并返回数据

public void GetIndustryList(out string result)
{
int count = 0;
string sort = string.IsNullOrEmpty(gRequest["sort"]) ? "rectime_11022" : gRequest["sort"];
string order = string.IsNullOrEmpty(gRequest["order"]) ? "desc" : gRequest["order"];
string sector = gRequest["sector"];
string name = gRequest["name"];
string sWhere = ""; if (!string.IsNullOrEmpty(sector) && sector != "请选择")
{
sWhere += " and f002v_10202='" + sector + "'";
} if (!string.IsNullOrEmpty(name))
{
sWhere += " and f004v_10202 like '" + name + "%'";
}
sWhere = sWhere.TrimStart(" and".ToCharArray());
BLL.vm_dms_allIndustry bll = new BLL.vm_dms_allIndustry();
List<Model.vm_dms_allIndustry> list = bll.GetListRowNumber("vm_dms_allIndustry", "", sWhere, GetPageIndex(), sort, order, GetPageSize(), "*", "f001g_10202", ref count);
string strResult = Newtonsoft.Json.JsonConvert.SerializeObject(list);
strResult = JsonHelper.JsonReplaceDate1(strResult);
strResult = "{ \"total\":" + count + ",\"rows\":" + strResult + "}";
result = strResult;
}

这样就可以避免写一堆 的switch case 了
类似于这种代码的可以不用写了

public class News_Manager : IHttpHandler
{ HttpRequest gRequest = null;
HttpContext gContext = null;
HttpResponse gResponse = null;
string func = string.Empty;
string result = string.Empty;
string pageUrl = string.Empty; public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
gContext = context;
gRequest = context.Request;
gResponse = context.Response;
pageUrl = gRequest.UrlReferrer.AbsolutePath;
func = gRequest["func"];
if (!string.IsNullOrEmpty(func))
{
switch (func)
{
case "Get_News_General_List":
Get_News_General_List(out result);
break;
case "News_General_Stock_Edit":
News_General_Stock_Edit(out result);
break;
case "Get_News_General_ById":
Get_News_General_ById(out result);
break;
case "News_General_Stock_Delete":
News_General_Stock_Delete(out result);
break;
case "Get_News_General_ByGuid":
Get_News_General_ByGuid(out result);
break;
case "News_General_Indus_Edit":
News_General_Indus_Edit(out result);
break;
case "News_General_Industry_Delete":
News_General_Industry_Delete(out result);
break;
case "Save":
Save(out result);
break;
case "GetNewsById":
GetNewsById(out result);
break;
case "Get_News_General_Industry_ById":
Get_News_General_Industry_ById(out result);
break;
case "CreateGuid":
CreateGuid(out result);
break;
case "GetStockByIndustry":
GetStockByIndustry(out result);
break;
case "GetPLByNewsIDAndType":
GetPLByNewsIDAndType(out result);
break;
default:
break;
}
}
gResponse.Write(result);
asp.net 使用JQuery 调用Ashx 后面直接写方法名,通过反射找到对应的方法的更多相关文章
- asp.net中js和jquery调用ashx的不同方法分享,需要的朋友可以参考一下
asp.net中js和jquery调用ashx的不同方法分享,需要的朋友可以参考一下 =============js================ 复制代码代码如下: var xhr = n ...
- 使用反射机制实现jQuery调用ashx类中的指定方法
使用反射机制实现jQuery调用ashx类中的指定方法 近期用asp.net做个小网站,但又不喜欢使用asp.net的服务器端控件,经过一番思量后确定前端采用原始的html.后台采用Linq to ...
- asp.net中js和jquery调用ashx的不同方法分享
代码如下: var xhr = new XMLHttpRequest(); xhr.open("get", 'Controls/gengCart.ashx?C ...
- C# 外界调用方法是 方法名是string类型的解决方法
- Jquery调用从ashx文件返回的jsonp格式的数据处理实例
开发环境:vs2010+jquery-1.4.min.js 解决问题:网上代码比较少,好多调试不通,返回数据不用json而用jsonp主要考虑解决跨域问题 开发步骤:打开VS2010,新建一web站点 ...
- jQuery Ajax 方法调用 Asp.Net WebService 以及调用aspx.cs中方法的详细例子
一.jQuery Ajax 方法调用 Asp.Net WebService (引自Terry Feng) Html文件 <!DOCTYPE html PUBLIC "-//W3C//D ...
- Asp.net中JQuery、ajax调用后台方法总结
通过上一篇文章实例的实现,整个过程当中学习到很多知识点,了解了Jquery.Ajax在asp.net中的运用,加以总结,其实原理都是一样的,理解了一种,其他的注意很少的区别就可以了.灵活运用: 1.有 ...
- 使用 jQuery 调用 ASP.NET AJAX Page Method
文章来源:http://chungle.iteye.com/blog/406054 说到轻量级的客户端通信,我注意到大多数人喜欢使用 ASP.NET AJAX Page Method 多于 ASMX ...
- jQuery调用Asp.Net后台方法
常用的ajax就不讲了,这里主要是说通过ajax调用asp.net后台的cs文件暴露的方法. 前台: <%@ Page Language="C#" AutoEventWire ...
随机推荐
- GCD异步加载网络图片
//image dispatch_queue_t network_queue; network_queue = dispatch_queue_create("com.myapp.networ ...
- 一个IT人士的个人经历,给迷失方向的朋友
这些日子我一直在写一个实时操作系统内核,已有小成了,等写完我会全部公开,希望能够为国内IT的发展尽自己一份微薄的力量.最近看到很多学生朋友和我当年一样没有方向 ,所以把我的经历写出来与大家共勉,希望能 ...
- POJ 1723
#include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; struct ...
- [STL]算法的泛化过程
“选择了错误的算法,便注定了失败的命运”.最近对这句话感触颇深,经常因为一开始思路错误,修改半天,到头来却都是无用功,所以学好算法势在必行. 算法的泛化过程 如何设计一个算法,使他适用于任何(大多数) ...
- android Notification定义与应用
首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...
- .net web部署(IIS Express && Nancy Self-Hosting)
http://d.hatena.ne.jp/fkmt5/20140330/1396195246 [1]Nancy Web配置注意事项 添加url:netsh http add urlacl url=h ...
- QTP之web常用对象
web对象是我做自动化以来最早学习,最早接触的.对现在而言也是最熟悉不过的了,但是为了以后更稳健的前进,对基础的东西搞扎实,相信以后的路会顺畅许多,下边简单汇总下web的常用几类对象: Browser ...
- Windows下获取高精度时间注意事项
Windows下获取高精度时间注意事项 [转贴 AdamWu] 花了很长时间才得到的经验,与大家分享. 1. RDTSC - 粒度: 纳秒级 不推荐优势: 几乎是能够获得最细粒度的计数器抛弃理由: ...
- Spring的父子容器问题
在ssm框架搭建的时候 配置了一个Spring容器,又配置了一个前端控制器 <!-- 初始化spring容器 --> <context-param> <param-nam ...
- linux下,如何把整个文件夹上传到服务器(另一台linux)
1.Linux下目录复制:本机->远程服务器 scp -r /home/shaoxiaohu/test1 zhidao@192.168.0.1:/home/test2 #test1为源目录, ...