Web API 下的 ActionFilter 與 ExceptionFilter

本文重點會標紅。
背景需求,在進入action以前作sign驗證,以及反正錯誤時,記錄log,返回錯誤的json形式信息。
#region  普通 ActionFilter add by caoheyang 20150319

    /// <summary>
    /// sign 以及參數合法性驗證過濾器 add by caoheyang 20150318
    /// </summary>
    public class SignOpenApiAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
        /// <summary>
        /// 重寫OnActionExecuting方法   在進入控制器以前驗證 sign以及 參數合法性信息 add by caoheyang 20150318
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            lock (actionContext)
            {
                dynamic paramodel = actionContext.ActionArguments["paramodel"]; //當前請求的參數對象 
                if (actionContext.ModelState.Count > 0 || paramodel == null) //參數錯誤,請求停止
                    actionContext.Response = actionContext.ActionDescriptor.ResultConverter.Convert
                            (actionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.ParaError));
                IGroupProvider groupProvider = new GroupProvider();
                GroupApiConfigModel groupCofigInfo = groupProvider.GetGroupApiConfigByAppKey(paramodel.app_key, paramodel.v).Data;
                if (groupCofigInfo != null && groupCofigInfo.IsValid == 1)//集團可用,且有appkey信息
                {
                    string signStr = groupCofigInfo.AppSecret + "app_key=" + paramodel.app_key + "timestamp" + paramodel.timestamp + "v=" + paramodel.v + groupCofigInfo.AppSecret;
                    string sign = MD5.Encrypt(signStr);
                    paramodel.group = ParseHelper.ToInt(groupCofigInfo.GroupId, 0);
                    actionContext.ActionArguments["paramodel"] = paramodel; ;
                    if (sign != paramodel.sign)   //sign錯誤,請求停止
                        actionContext.Response = actionContext.ActionDescriptor.ResultConverter.Convert
                            (actionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.SignError)); //此處標紅須要與 apicontroller裏應用fillter的action返回值一直。
                }
                else
                    actionContext.Response = actionContext.ActionDescriptor.ResultConverter.Convert
                           (actionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.SignError));  //sign錯誤,請求停止
            }
        }
    }

    #endregion

    #region  ExceptionFilter add by caoheyang 20150319

    /// <summary>
    /// 自定義全局異常處理類  add by caoheyang 20150319   全局過濾器,記錄log /// </summary>
    public class OpenApiHandleErrorAttribute : ExceptionFilterAttribute
    {
        /// <summary>
        /// 重寫異常處理方法 add by caoheyang 20150205  其次執行 /// </summary>
        /// <param name="filterContext">上下文對象  該類繼承於ControllerContext</param>
        public override void OnException(HttpActionExecutedContext filterContext)
        {
            LogHelper.LogWriterFromFilter(filterContext.Exception);
        }
    }

    /// <summary>  action過濾器,當內部代碼發生異常時,改變當前的請求服務器端返回信息
    /// 自定義action異常處理類,捕獲異常,返回系統錯誤提示信息  add by caoheyang 20150319
    /// </summary>
    public class OpenApiActionErrorAttribute : ExceptionFilterAttribute
    {
        /// <summary>
        /// 重寫異常處理方法 add by caoheyang 20150205   首先執行  
        /// </summary>
        /// <param name="filterContext">上下文對象  該類繼承於ControllerContext</param>
        public override void OnException(HttpActionExecutedContext filterContext)
        {
            filterContext.Response = filterContext.ActionContext.ActionDescriptor.ResultConverter.
              Convert(filterContext.ActionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.SystemError));
            //此處標紅須要與 apicontroller裏應用fillter的action返回值一直。
} } #endregion

ApiController eg。json

 // POSR: Order GetStatus    paramodel 固定 必須是 paramodel
        /// <summary>
        /// 訂單狀態查詢功能  add by caoheyang 20150316
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [SignOpenApi] //sign驗證過濾器 設計參數驗證,sign驗證 add by caoheyang 201503167
        [OpenApiActionError] //異常過濾器 add by caoheyang 一旦發生異常,客戶端返回系統內部錯誤提示
        public ResultModel<object> GetStatus(ParaModel<GetStatusPM_OpenApi> paramodel)
        {
          
        }
 
相關文章
相關標籤/搜索