예제 #1
0
 void IXmlSerializable.WriteXml(XmlWriter writer)
 {
     foreach (KeyValuePair <string, object> keyValuePair in this)
     {
         string key   = keyValuePair.Key;
         object value = keyValuePair.Value;
         writer.WriteStartElement(XmlConvert.EncodeLocalName(key));
         if (value != null)
         {
             HttpErrorNegareh innerError = value as HttpErrorNegareh;
             if (innerError == null)
             {
                 writer.WriteValue(value);
             }
             else
             {
                 ((IXmlSerializable)innerError).WriteXml(writer);
             }
         }
         writer.WriteEndElement();
     }
 }
예제 #2
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);
        }
예제 #3
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);
        }