Пример #1
0
        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);
        }