c# web项目请求样例

发布时间:2020-08-11 12:25:51 作者:MachealChen
来源:网络 阅读:514


   


using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Web;
using System.ComponentModel;
using System.Web.Script.Serialization;
namespace HHSoft.PSMIS.Web.WebSite.UserHandler
{
    /// <summary>
    /// HandlerBase 的摘要说明
    /// </summary>
    public class HandlerBase : IHttpHandler
    {
        /// <summary>
        /// 指定过来的http请求类型  主要指定action方法名称的接收方式 get 或者 post
        /// </summary>
        protected NameValueCollection httpReuqest = HttpContext.Current.Request.Form;
        /// <summary>
        /// 指定返回头
        /// </summary>
        protected string contentType = "text/plain";
        /// <summary>
        /// 返回值类型
        /// </summary>
        public ReturnType returnType = ReturnType.json;
        /// <summary>
        /// 指定接收action方法的参数名称
        /// </summary>
        protected string actionName = "action";
        //获取当前的http context
        protected HttpContext Context
        {
            get
            {
                return HttpContext.Current;
            }
        }
        public void Proce***equest(HttpContext context)
        {
            //RequestType = context.Request.ServerVariables["Request_Method"];
            string requestContentType ="";
            string requestReturnType="";
            requestContentType = httpReuqest["contentType"];
            requestReturnType = httpReuqest["returnType"];
            if (!string.IsNullOrEmpty(requestReturnType))
            {
                returnType = (ReturnType)Enum.Parse(typeof(ReturnType), requestReturnType);
            }
            if (string.IsNullOrEmpty(requestContentType))
            {
                context.Response.ContentType = this.contentType;
            }
            else
            {
                context.Response.ContentType = requestContentType;
            }
            try
            {
                //动态调用方法 当然  你还可以在这里加上是否为同域名请求的判断
                this.DynamicMethod();
            }
            catch (AmbiguousMatchException amEx)
            {
                                      
                if (returnType == ReturnType.json)
                {
                    this.PrintErrorJson(string.Format("根据该参数{0}找到了多个方法", amEx.Message));
                }
                else
                {
                    this.Context.Response.Write(string.Format("error,根据该参数{0}找到了多个方法", amEx.Message));
                }
            }
            catch (ArgumentException argEx)
            {
                if (returnType == ReturnType.json)
                {
                    this.PrintErrorJson("参数异常" + argEx.Message);
                }
                else
                {
                    this.Context.Response.Write("error,参数异常" + argEx.Message);
                }
                                     
            }
            catch (ApplicationException apEx)
            {
                if (returnType == ReturnType.json)
                {
                    this.PrintErrorJson("程序异常" + apEx.Message);
                }
                else
                {
                    this.Context.Response.Write("error,程序异常" + apEx.Message);
                }
                                     
            }
        }
        #region 动态调用方法
        /// <summary>
        /// 动态调用方法
        /// </summary>
        private void DynamicMethod()
        {
            //根据指定的请求类型获取方法名
            string action = this.httpReuqest[this.actionName];
            if (!string.IsNullOrEmpty(action))
            {
                //获取方法的实例  非静态 需要Public访问权限 忽略大小写
                MethodInfo methodInfo = this.GetType().GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (methodInfo != null)
                {
                    //调用方法
                    methodInfo.Invoke(this, null);
                }
                else
                {
                    throw new ApplicationException(string.Format("没有找到方法{0}", action));
                }
            }
            else
            {
                throw new ArgumentNullException("没有找到调用方法参数或者方法名为空");
            }
        }
        #endregion
        #region 打印Json的相关处理
        #region 打印Json的相关处理
        /// <summary>
        /// 打印遇到异常的json
        /// </summary>
        /// <param name="msg"></param>
        protected void PrintErrorJson(string msg)
        {
            this.PrintJson("error", msg);
        }
        /// <summary>
        /// 打印成功处理的json
        /// </summary>
        /// <param name="msg"></param>
        protected void PrintSuccessJson(string msg)
        {
            this.PrintJson("success", msg);
        }
        /// <summary>
        /// 打印json
        /// </summary>
        /// <param name="state"></param>
        /// <param name="msg"></param>
        protected void PrintJson(string state, string msg)
        {
            this.Context.Response.Write("{\"state\":\"" + state + "\",\"msg\":\"" + msg + "\"}");
        }
        protected void PrintString(string obj)
        {
            this.Context.Response.Write(obj);
        }
        #endregion
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public enum ReturnType
    {
        /// <summary>
        /// 返回字符串
        /// </summary>
        [Description("返回字符串")]
        text=0,
        /// <summary>
        /// 返回json类型
        /// </summary>
        [Description("返回json类型")]
        json=1
    }
}执勤啊
d
/// <summary>
   /// HandlerBase 的摘要说明
   /// </summary>
   public class HandlerBase : IHttpHandler
   {
       /// <summary>
       /// 指定过来的http请求类型  主要指定action方法名称的接收方式 get 或者 post
       /// </summary>
       protected NameValueCollection httpReuqest = HttpContext.Current.Request.Form;
       /// <summary>
       /// 指定返回头
       /// </summary>
       protected string contentType = "text/plain";
       /// <summary>
       /// 返回值类型
       /// </summary>
       public ReturnType returnType = ReturnType.json;
       /// <summary>
       /// 指定接收action方法的参数名称
       /// </summary>
       protected string actionName = "action";
       //获取当前的http context
       protected HttpContext Context
       {
           get
           {
               return HttpContext.Current;
           }
       }
       public void Proce***equest(HttpContext context)
       {
           //RequestType = context.Request.ServerVariables["Request_Method"];
           string requestContentType ="";
           string requestReturnType="";
           requestContentType = httpReuqest["contentType"];
           requestReturnType = httpReuqest["returnType"];
           if (!string.IsNullOrEmpty(requestReturnType))
           {
               returnType = (ReturnType)Enum.Parse(typeof(ReturnType), requestReturnType);
           }
           if (string.IsNullOrEmpty(requestContentType))
           {
               context.Response.ContentType = this.contentType;
           }
           else
           {
               context.Response.ContentType = requestContentType;
           }
           try
           {
               //动态调用方法 当然  你还可以在这里加上是否为同域名请求的判断
               this.DynamicMethod();
           }
           catch (AmbiguousMatchException amEx)
           {
                                                  
               if (returnType == ReturnType.json)
               {
                   this.PrintErrorJson(string.Format("根据该参数{0}找到了多个方法", amEx.Message));
               }
               else
               {
                   this.Context.Response.Write(string.Format("error,根据该参数{0}找到了多个方法", amEx.Message));
               }
           }
           catch (ArgumentException argEx)
           {
               if (returnType == ReturnType.json)
               {
                   this.PrintErrorJson("参数异常" + argEx.Message);
               }
               else
               {
                   this.Context.Response.Write("error,参数异常" + argEx.Message);
               }
                                                 
           }
           catch (ApplicationException apEx)
           {
               if (returnType == ReturnType.json)
               {
                   this.PrintErrorJson("程序异常" + apEx.Message);
               }
               else
               {
                   this.Context.Response.Write("error,程序异常" + apEx.Message);
               }
                                                 
           }
       }
       #region 动态调用方法
       /// <summary>
       /// 动态调用方法
       /// </summary>
       private void DynamicMethod()
       {
           //根据指定的请求类型获取方法名
           string action = this.httpReuqest[this.actionName];
           if (!string.IsNullOrEmpty(action))
           {
               //获取方法的实例  非静态 需要Public访问权限 忽略大小写
               MethodInfo methodInfo = this.GetType().GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
               if (methodInfo != null)
               {
                   //调用方法
                   methodInfo.Invoke(this, null);
               }
               else
               {
                   throw new ApplicationException(string.Format("没有找到方法{0}", action));
               }
           }
           else
           {
               throw new ArgumentNullException("没有找到调用方法参数或者方法名为空");
           }
       }
       #endregion
       #region 打印Json的相关处理
       #region 打印Json的相关处理
       /// <summary>
       /// 打印遇到异常的json
       /// </summary>
       /// <param name="msg"></param>
       protected void PrintErrorJson(string msg)
       {
           this.PrintJson("error", msg);
       }
       /// <summary>
       /// 打印成功处理的json
       /// </summary>
       /// <param name="msg"></param>
       protected void PrintSuccessJson(string msg)
       {
           this.PrintJson("success", msg);
       }
       /// <summary>
       /// 打印json
       /// </summary>
       /// <param name="state"></param>
       /// <param name="msg"></param>
       protected void PrintJson(string state, string msg)
       {
           this.Context.Response.Write("{\"state\":\"" + state + "\",\"msg\":\"" + msg + "\"}");
       }
       protected void PrintString(string obj)
       {
           this.Context.Response.Write(obj);
       }
       #endregion
       #endregion
       public bool IsReusable
       {
           get
           {
               return false;
           }
       }
   }
   public enum ReturnType
   {
       /// <summary>
       /// 返回字符串
       /// </summary>
       [Description("返回字符串")]
       text=0,
       /// <summary>
       /// 返回json类型
       /// </summary>
       [Description("返回json类型")]
       json=1
   }
之前
推荐阅读:
  1. 怎么在C#中实现WebApi框架
  2. 使用C#怎么对WebApi 接口进行传参

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

项目 public 样例

上一篇:英文单词缩写----DXNRY – Dictionary 字典

下一篇:大数运算实现加减乘除四则运算

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》