public static void Handle() { var successFailed = new Dictionary<string, object>(); try { var qs = HttpContext.Current.Request.QueryString[0]; var dictionaryList = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(qs); var nameValue = new NameValueCollection(); foreach (var item in dictionaryList) { nameValue.Add(item.First().Value, item.Last().Value); } var redirectAfterHandle = new BasketRequestHandler().HandleBasketRequest(nameValue, HttpContext.Current.Request.UrlReferrer); var lastItem = redirectAfterHandle.Last(); if (!successFailed.ContainsKey(lastItem.Action)) { successFailed.Add("success", redirectAfterHandle.All(x => x.Success)); successFailed.Add("validated", redirectAfterHandle.All(x => x.Validated)); var messageDictionary = new Dictionary<string, string>(); foreach (var message in redirectAfterHandle.SelectMany(handleItem => handleItem.Messages.Where(message => !messageDictionary.ContainsKey(message.Key)))) { messageDictionary.Add(message.Key, message.Value); } successFailed.Add("messages", messageDictionary); successFailed.Add("url", lastItem.Url); successFailed.Add("item", lastItem.Item); } } catch (Exception ex) { successFailed = new Dictionary<string, object>(); var messages = new Dictionary<string, string> {{"exception", ex.ToString()}}; successFailed.Add("success", false); successFailed.Add("validated", false); successFailed.Add("url", string.Empty); successFailed.Add("error", "something went wrong with the Handle method: "); successFailed.Add("messages", messages); } var serializeSettings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = GlobalSettings.DebugMode ? Formatting.Indented : Formatting.None }; var json = JsonConvert.SerializeObject(successFailed, Formatting.Indented, serializeSettings); HttpContext.Current.Response.ContentType = "application/json"; HttpContext.Current.Response.Write(json); }
public static string RenderOutput(string methodName, object objectToSerialze, bool renderXMLInteadofJSON = false) { var successFailedObject = new Dictionary<string, object>(); try { if (objectToSerialze != null) { successFailedObject.Add("item", objectToSerialze); successFailedObject.Add("success", true); } else { successFailedObject.Add("error", "something went wrong with " + methodName); successFailedObject.Add("success", false); } successFailedObject.Add("messages", new Dictionary<string, string>()); successFailedObject.Add("validated", false); successFailedObject.Add("url", string.Empty); } catch (Exception ex) { successFailedObject = new Dictionary<string, object>(); var messages = new Dictionary<string, string> { { "exception", ex.ToString() } }; successFailedObject.Add("error", "something went wrong with " + methodName); successFailedObject.Add("messages", messages); successFailedObject.Add("success", false); successFailedObject.Add("validated", false); successFailedObject.Add("url", string.Empty); } //todo: formatting NONE als umbraco NIET in debug draait var serializeSettings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented }; if (renderXMLInteadofJSON) { var xmlObjectAsJson = JsonConvert.SerializeObject(objectToSerialze, Formatting.Indented, serializeSettings); return XMLoutput(xmlObjectAsJson).OuterXml; } var json = JsonConvert.SerializeObject(successFailedObject, Formatting.Indented, serializeSettings); JSONoutput(json); return string.Empty; }