zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    计算机视觉概要和卷积神经网络基础概念
    详解类别不平衡问题
    机器学习中几种常见的模型评价指标
    目标检测中常用的IOU、NMS和mAP
    Trie树(字典树)的C++实现
    台大林轩田老师《机器学习技法》课程笔记2:Combining Predictive Features: Aggregation Models
    台大林轩田老师《机器学习技法》课程笔记1:Embedding Numerous Features: Kernel Models
    利用OpenCV在图片和摄像头中检测人脸
    台大林轩田老师《机器学习基石》课程笔记4:How Can Machines Learn Better?
    台大林轩田老师《机器学习基石》课程笔记3:How can machines learn?
  • 原文地址:https://www.cnblogs.com/hjtdlx/p/4060909.html
Copyright © 2011-2022 走看看