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 后面直接写方法名,通过反射找到对应的方法的更多相关文章

  1. asp.net中js和jquery调用ashx的不同方法分享,需要的朋友可以参考一下

    asp.net中js和jquery调用ashx的不同方法分享,需要的朋友可以参考一下     =============js================ 复制代码代码如下: var xhr = n ...

  2. 使用反射机制实现jQuery调用ashx类中的指定方法

    使用反射机制实现jQuery调用ashx类中的指定方法   近期用asp.net做个小网站,但又不喜欢使用asp.net的服务器端控件,经过一番思量后确定前端采用原始的html.后台采用Linq to ...

  3. asp.net中js和jquery调用ashx的不同方法分享

    代码如下: var xhr = new XMLHttpRequest();            xhr.open("get", 'Controls/gengCart.ashx?C ...

  4. C# 外界调用方法是 方法名是string类型的解决方法

  5. Jquery调用从ashx文件返回的jsonp格式的数据处理实例

    开发环境:vs2010+jquery-1.4.min.js 解决问题:网上代码比较少,好多调试不通,返回数据不用json而用jsonp主要考虑解决跨域问题 开发步骤:打开VS2010,新建一web站点 ...

  6. jQuery Ajax 方法调用 Asp.Net WebService 以及调用aspx.cs中方法的详细例子

    一.jQuery Ajax 方法调用 Asp.Net WebService (引自Terry Feng) Html文件 <!DOCTYPE html PUBLIC "-//W3C//D ...

  7. Asp.net中JQuery、ajax调用后台方法总结

    通过上一篇文章实例的实现,整个过程当中学习到很多知识点,了解了Jquery.Ajax在asp.net中的运用,加以总结,其实原理都是一样的,理解了一种,其他的注意很少的区别就可以了.灵活运用: 1.有 ...

  8. 使用 jQuery 调用 ASP.NET AJAX Page Method

    文章来源:http://chungle.iteye.com/blog/406054 说到轻量级的客户端通信,我注意到大多数人喜欢使用 ASP.NET AJAX Page Method 多于 ASMX ...

  9. jQuery调用Asp.Net后台方法

    常用的ajax就不讲了,这里主要是说通过ajax调用asp.net后台的cs文件暴露的方法. 前台: <%@ Page Language="C#" AutoEventWire ...

随机推荐

  1. Feature Extraction

    http://www.erogol.com/ml-work-flow-part-3-feature-extraction/

  2. nenu contest3

    http://vjudge.net/contest/view.action?cid=55702#overview 12656 - Almost Palindrome http://uva.online ...

  3. linux源代码阅读笔记 高速缓冲区管理

    高速缓冲区是文件系统访问块设备中数据的必经要道,为了访问文件系统等块设备上的数据,内核可以每次都访问块设备,进行读写操作. 为了提高系统性能,内核在内存中开辟一个高速数据缓冲区.在Linux内核中,高 ...

  4. js正则函数match、exec、test、search、replace、split使用介绍

    match() 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) stringObj 必选项.对其进行查找的 String 对象或字符 ...

  5. 关于dynamic_cast

    http://www.groad.net/bbs/read.php?tid-5476.html dynamic_cast 进行运行时强制转换.如果在执行时转换非法,则会转换失败.dynamic_cas ...

  6. 使用getJSON()方法异步加载JSON格式数据

    使用getJSON()方法异步加载JSON格式数据 使用getJSON()方法可以通过Ajax异步请求的方式,获取服务器中的数组,并对获取的数据进行解析,显示在页面中,它的调用格式为: jQuery. ...

  7. flexbox弹性盒子布局

    混合划分 demo1,css: #demo1{ width: 100%; background: #ccc; display: -webkit-flex;/*表示使用弹性布局*/ } #demo1 . ...

  8. Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  9. 使用CXF暴露您的REST服务

    使用CXF暴露您的REST服务 REST应用服务器SpringBeanServlet  1.  前言 现在互联网Open API流行,将您的Web应用也可以开放Open API给其他第三方使用.达到一 ...

  10. NSDictionary 遍历

           NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys: @"1",@"a", ...