c# web項目請求樣例


   


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
   }
之前
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章