Esempio n. 1
0
        /// <summary>
        /// 在执行操作方法之前由 ASP.NET MVC 框架调用。
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext == null)
            {
                return;
            }

            #region 检测ActionResult返回类型

            #region 检测是否已经在Action或Controller上设置 CustomErrorAttribute

            Type customErrorAttributeType = typeof(CustomErrorAttribute);

            object[] attributes = null;

            if (filterContext.ActionDescriptor.IsDefined(customErrorAttributeType, true))
            {
                attributes = filterContext.ActionDescriptor.GetCustomAttributes(customErrorAttributeType, true);
            }
            else if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(customErrorAttributeType, true))
            {
                attributes = filterContext.ActionDescriptor.GetCustomAttributes(customErrorAttributeType, true);
            }

            if (attributes == null)
            {
                #region 如果没有设置CustomErrorAttribute,则使用最终的相应格式输出数据

                var actionDescriptor = filterContext.ActionDescriptor as IMethodInfoActionDescriptor;
                var actionReturnType = actionDescriptor.MethodInfo.ReturnType;

                if (object.ReferenceEquals(typeof(JsonResult), actionReturnType))
                {
                    this.Format = EnumOutputFormat.JSON;
                }
                else
                {
                    this.Format = EnumOutputFormat.Redirect;
                }

                #endregion
            }
            else
            {
                this.Format = ((CustomErrorAttribute)attributes.Last()).Format;
            }

            #endregion

            #endregion

            base.OnActionExecuting(filterContext);
        }
Esempio n. 2
0
        //*** Private methods ***************************************************************************************************/

        /// <summary>
        /// Return string that contains formatted matrix data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="matrix"></param>
        /// <returns></returns>
        private static string DisplayMatrix2D <T>(T[,] matrix, EnumOutputFormat enumOutputType)
        {
            string stringOut = string.Empty;

            for (int i = 0; i <= matrix.GetUpperBound(0); i++)
            {
                StringBuilder stringBuilder = new StringBuilder();
                for (int y = 0; y <= matrix.GetUpperBound(1); y++)
                {
                    switch (enumOutputType)
                    {
                    case EnumOutputFormat.Integer:
                        stringBuilder.Append($"{matrix[i, y]:0} ");
                        break;

                    case EnumOutputFormat.Double1:
                        stringBuilder.Append($"{matrix[i, y],CommonItems.intPadOutput:0.0} ");
                        break;

                    case EnumOutputFormat.Double2:
                        stringBuilder.Append($"{matrix[i, y],CommonItems.intPadOutput:0.00} ");
                        break;

                    case EnumOutputFormat.Double3:
                        stringBuilder.Append($"{matrix[i, y],CommonItems.intPadOutput:0.000} ");
                        break;

                    case EnumOutputFormat.Double4:
                        stringBuilder.Append($"{matrix[i, y],CommonItems.intPadOutput:0.0000} ");
                        break;
                    }
                }
                if (enumOutputType.Equals(EnumOutputFormat.Integer))
                {
                    stringOut += $"\n           {stringBuilder}";      // Offest integer output with spaces to align with after decimal in double output.
                }
                else
                {
                    stringOut += $"\n{stringBuilder}";
                }
            }
            stringOut += "\n";
            return(stringOut);
        }
Esempio n. 3
0
 /// <summary>
 /// 相应格式(跳转或JSON)
 /// </summary>
 /// <param name="format"></param>
 public CustomErrorAttribute(EnumOutputFormat format)
 {
     this.Format = format;
 }
Esempio n. 4
0
 public CustomErrorAttribute()
 {
     Format = EnumOutputFormat.Redirect;
 }