-
-
Save tugberkugurlu/4025019 to your computer and use it in GitHub Desktop.
Deserializing HttpError
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class HttpErrorExtensions | |
| { | |
| public static ModelStateDictionary GetModelState(this HttpError httpError) | |
| { | |
| Ensure.Argument.NotNull(httpError, "httpError"); | |
| object serialized; | |
| if (httpError.TryGetValue("ModelState", out serialized)) | |
| { | |
| var modelState = new ModelStateDictionary(); | |
| if (serialized is JObject) | |
| { | |
| GetModelStateFromJObject((JObject)serialized, modelState); | |
| } | |
| else if (serialized is IDictionary<string, object>) | |
| { | |
| GetModelStateFromDictionary((IDictionary<string, object>)serialized, modelState); | |
| } | |
| return modelState; | |
| } | |
| return null; | |
| } | |
| private static void GetModelStateFromJObject(JObject container, ModelStateDictionary modelState) | |
| { | |
| foreach (var error in container) | |
| { | |
| foreach (var message in error.Value.Values<string>()) | |
| { | |
| modelState.AddModelError(error.Key, message); | |
| } | |
| } | |
| } | |
| private static void GetModelStateFromDictionary(IDictionary<string, object> dictionary, ModelStateDictionary modelState) | |
| { | |
| foreach (var error in dictionary) | |
| { | |
| foreach (var message in error.Value as string[]) | |
| { | |
| modelState.AddModelError(error.Key, message); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment