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 ...
随机推荐
- WinForm中Component Class、User Control及Custom Control的区别和使用建议
reference: http://blog.csdn.net/redstonehe/article/details/1536549 .NET Framework 为您提供了开发和实现新控件的能力.除 ...
- iOS开发之深入探讨runtime机制01-类与对象
最近有个同事问我关于“runtime机制”的问题,我想可能很多人对这个都不是太清楚,在这里,和大家分享一下我对于runtime机制的理解.要深入理解runtime,首先要从最基本的类与对象开始,本文将 ...
- uva 11174
刘书上例题 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...
- Unity3D脚本中文系列教程(五)
http://dong2008hong.blog.163.com/blog/static/4696882720140302848544/?suggestedreading&wumii Unit ...
- VS2010 MFC DataGrid绑定实例
VS2010环境下MFC使用DataGrid绑定数据源 参考:http://blog.csdn.net/fddqfddq/article/details/7874706 详细介绍如何在MFC中使用Da ...
- C#画图
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- HDU 1573 X问题 (中国剩余定理)
题目链接 题意 : 中文题不详述. 思路 : 中国剩余定理.求中国剩余定理中解的个数.看这里看这里 #include <stdio.h> #include <iostream> ...
- Java学习笔记(一) java介绍
编程语言分为:编译型语言和解释型语言. 编译型语言需要经过特定编译器通过一次性编译,成为该特定平台硬件可执行的机器码,可脱离开发环境独立运行,运行效率较高,但是无法跨平台移植. 解释型语言需要经过特定 ...
- @Override在JDK1.5和JDK1.6中用法区别
@Override 注解在jdk1.5环境下,只能用于对基类(父类)的方法的重写.而不能用于对实现的接口的方法的实现.而在jdk1.6环境下,两者都适用.
- 银联接口(注意项&备忘)
1,参考文档“证书下载.导出及上传流程.docx” 按照文档上所述,依次进行,导出的证书备用,用于配置文件的项“const SDK_ENCRYPT_CERT_PATH” 2,使用tp框架 新建一个控制 ...