WebApi异常过滤器

 public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var x = actionExecutedContext.Exception;
            Trace.TraceError(x.ToString());
            actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.OK);
            actionExecutedContext.Response.Content = new ObjectContent<ApiResult>(ApiResult.ret((int)ApiResult.enum_result.fail, filter(x.Message), null), new System.Net.Http.Formatting.JsonMediaTypeFormatter());

        }
private string filter(string m_strError)
        {
            if (string.IsNullOrEmpty(m_strError) || m_strError.ToLower().Equals("null"))
                return string.Empty;
            Regex reg = new Regex("ORA-(.+):");
            m_strError = reg.Replace(m_strError, "");
            reg = new Regex("at(.+)");
            m_strError = reg.Replace(m_strError, "");
            reg = new Regex("在 \"I~H~M~S2014_TG_LOGIN(.+)");
            m_strError = reg.Replace(m_strError, "");
            reg = new Regex("在 \"line(.+)");
            m_strError = reg.Replace(m_strError, "");
            reg = new Regex(@"\n \n 在 line 1");
            m_strError = reg.Replace(m_strError, "");
            reg = new Regex(@"\n(.+)");
            m_strError = reg.Replace(m_strError, "");
            return m_strError;
        }

 

WebApi异常过滤器

上一篇:后台返回API数据格式


下一篇:批处理工具CAPI逆向分析之API Call