예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpErrorNegareh"/> class for <paramref name="modelState"/>.
        /// </summary>
        /// <param name="modelState">The invalid model state to use for error information.</param>
        /// <param name="includeErrorDetail"><c>true</c> to include exception messages in the error; <c>false</c> otherwise</param>
        public HttpErrorNegareh(ModelStateDictionary modelState, bool includeErrorDetail)
            : this()
        {
            if (modelState == null)
            {
                throw Error.ArgumentNull("modelState");
            }

            if (modelState.IsValid)
            {
                throw Error.Argument("modelState", "The model state is valid." /*SRResources.ValidModelState*/);
            }

            Message = "The request is invalid.";// SRResources.BadRequest;

            HttpErrorNegareh modelStateError = new HttpErrorNegareh();

            foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState)
            {
                string key   = keyModelStatePair.Key;
                var    index = key.LastIndexOf('.');
                key = key.Substring(index + 1, key.Length - index - 1);

                ModelErrorCollection errors = keyModelStatePair.Value.Errors;
                if (errors != null && errors.Count > 0)
                {
                    IEnumerable <string> errorMessages = errors.Select(error =>
                    {
                        if (includeErrorDetail && error.Exception != null)
                        {
                            return(error.Exception.Message);
                        }
                        else
                        {
                            return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage);
                        }
                    }).ToArray();
                    modelStateError.Add(key, errorMessages);
                }
            }
            Add("NegarehErrorState", modelStateError);

            modelStateError = new HttpErrorNegareh();
            foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState)
            {
                string key = keyModelStatePair.Key;

                ModelErrorCollection errors = keyModelStatePair.Value.Errors;
                if (errors != null && errors.Count > 0)
                {
                    IEnumerable <string> errorMessages = errors.Select(error =>
                    {
                        if (includeErrorDetail && error.Exception != null)
                        {
                            return(error.Exception.Message);
                        }
                        else
                        {
                            return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage);
                        }
                    }).ToArray();
                    modelStateError.Add(key, errorMessages);
                }
            }
            Add(HttpErrorKeys.ModelStateKey, modelStateError);
        }
예제 #2
0
        public HttpErrorNegareh(ModelStateDictionary modelState, bool includeErrorDetail, params string[] modelNames)
            : this()
        {
            if (modelState == null)
            {
                throw Error.ArgumentNull("modelState");
            }

            if (modelState.IsValid)
            {
                throw Error.Argument("modelState", "The model state is valid." /*SRResources.ValidModelState*/);
            }

            Message = "The request is invalid.";// SRResources.BadRequest;

            HttpErrorNegareh        modelStateError  = new HttpErrorNegareh();
            List <HttpErrorNegareh> modelStateError2 = new List <HttpErrorNegareh>();

            foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState)
            {
                if (!IsModel(keyModelStatePair.Key, modelNames))
                {
                    string key   = keyModelStatePair.Key;
                    var    index = key.LastIndexOf('.');
                    key = key.Substring(index + 1, key.Length - index - 1);

                    ModelErrorCollection errors = keyModelStatePair.Value.Errors;
                    if (errors != null && errors.Count > 0)
                    {
                        IEnumerable <string> errorMessages = errors.Select(error =>
                        {
                            if (includeErrorDetail && error.Exception != null)
                            {
                                return(error.Exception.Message);
                            }
                            else
                            {
                                return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage);
                            }
                        }).ToArray();
                        modelStateError.Add(key, errorMessages);
                    }
                }
                else
                {
                    var akey = keyModelStatePair.Key.Split('.');

                    string key   = keyModelStatePair.Key;
                    var    index = key.LastIndexOf('.');
                    key = key.Substring(index + 1, key.Length - index - 1);

                    ModelErrorCollection errors = keyModelStatePair.Value.Errors;
                    if (errors != null && errors.Count > 0)
                    {
                        IEnumerable <string> errorMessages = errors.Select(error =>
                        {
                            if (includeErrorDetail && error.Exception != null)
                            {
                                return(error.Exception.Message);
                            }
                            else
                            {
                                return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage);
                            }
                        }).ToArray();
                        HttpErrorNegareh tmodelStateError = new HttpErrorNegareh();
                        tmodelStateError.Add(key, errorMessages);
                        modelStateError2.Add(tmodelStateError);
                    }
                    //key
                }
            }
            if (modelStateError2.Count() > 0)
            {
                HttpErrorNegareh modelStateErrorGrafLink = new HttpErrorNegareh();
                for (int i = 0; i < modelStateError2.Count(); i++)
                {
                    modelStateErrorGrafLink.Add($"{ i }", modelStateError2[i]);/*{ modelNames[0] }_*/
                }
                //var ar = modelStateError2.ToArray();
                modelStateError.Add("GraftLink", /*modelStateError2*/ modelStateErrorGrafLink);
            }
            Add("NegarehErrorState", modelStateError);

            modelStateError = new HttpErrorNegareh();
            foreach (KeyValuePair <string, ModelState> keyModelStatePair in modelState)
            {
                string key = keyModelStatePair.Key;

                ModelErrorCollection errors = keyModelStatePair.Value.Errors;
                if (errors != null && errors.Count > 0)
                {
                    IEnumerable <string> errorMessages = errors.Select(error =>
                    {
                        if (includeErrorDetail && error.Exception != null)
                        {
                            return(error.Exception.Message);
                        }
                        else
                        {
                            return(String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred." /*SRResources.ErrorOccurred*/ : error.ErrorMessage);
                        }
                    }).ToArray();
                    modelStateError.Add(key, errorMessages);
                }
            }
            Add(HttpErrorKeys.ModelStateKey, modelStateError);
        }