Ejemplo n.º 1
0
        public ApiResponse(HttpStatusCode statusCode, string type = null, string errorMessage        = null, Exception exc = null,
                           ModelStateDictionary modelState        = null, ApiResponseModel <T> model = default(ApiResponseModel <T>))
        {
            //
            this._errors     = new List <Error>();
            this._warnings   = new List <Warning>();
            this._model      = model;
            this._statusCode = statusCode;

            if (model != null)
            {
                this._data = this._model.data;
                this._model.data.pagination.recordPerPage = (Convert.ToInt32(this._model.data.pagination.totalCount) > 0) ? AppSettings.GetByKey("PageSize") : "0";
            }

            if (HttpContext.Current.Items["warningmessage"] != null)
            {
                this._warnings.Add(new Warning()
                {
                    type = type, message = new List <string>()
                    {
                        HttpContext.Current.Items["warningmessage"].ToString()
                    }
                });
            }


            #region Incase of Error and Unauthorization

            if (!string.IsNullOrEmpty(errorMessage))
            {
                this._errors.Add(new Error()
                {
                    type = type, message = new List <string>()
                    {
                        errorMessage
                    }
                });
            }

            #endregion

            #region Incase of Exception

            if (exc != null)
            {
                string innerException = exc.InnerException != null ? exc.InnerException.Message : string.Empty;
                this._errors.Add(new Error()
                {
                    type    = type,
                    message = new List <string>()
                    {
                        string.Format("Message : {0}", exc.Message),
                        string.Format("Source: {0}", exc.Source),
                        string.Format("InnerException: {0}", innerException)
                    }
                });
            }

            #endregion

            #region Incase of ModelState InValid for Validation

            if (modelState != null)
            {
                if (modelState.Count() > 0)
                {
                    foreach (var keyValue in modelState)
                    {
                        this._errors.Add(new Error()
                        {
                            type = type, message = new List <string>()
                            {
                                string.Format("{0}", keyValue.Value.Errors.FirstOrDefault().ErrorMessage),                                                                   /* string.Format("{0}", keyValue.Value.Errors.FirstOrDefault().Exception)*/
                            }
                        });
                    }
                }
            }

            #endregion

            #region Incase of Success
            //there is no need to do anything, all set already for success
            #endregion
        }
Ejemplo n.º 2
0
 public ApiResponseModel()
 {
     data = new data <T>();
 }