public HomeModule() { Get["/"] = p => { var configuration = Nancy.TinyIoc.TinyIoCContainer.Current.Resolve<ServantConfiguration>(); var latestErrors = EventLogHelper.GetByDateTimeDescending(5).ToList(); latestErrors = EventLogHelper.AttachSite(latestErrors, Page.Sites); Model.UnhandledExceptions = latestErrors; Model.HaveSeenNewsletter = configuration.HaveSeenNewsletter; return View["Index", Model]; }; Post["/subscribetonewsletter"] = p => { var email = Request.Form.Email; var firstname = Request.Form.Firstname; var lastname = Request.Form.Lastname; try { new System.Net.Mail.MailAddress(email); } catch { AddPropertyError("email", "Looks like the email is not valid."); } var serializer = new Nancy.Json.JavaScriptSerializer(); if (HasErrors) { return new { Type = MessageType.Error.ToString(), Errors = serializer.Serialize(Model.Errors) }; ; } var response = MailchimpHelper.Subscribe(email, firstname, lastname); if (response != "true") { MailchimpResponse result = serializer.Deserialize<MailchimpResponse>(response); if (result.Code == 214) { SetNewsletterRead(); } if (result.Code == 502) { AddPropertyError("email", "Looks like the email is not valid."); return new { Type = MessageType.Error, Errors = serializer.Serialize(Model.Errors) }; } return new { Message = result.Error, Type = MessageType.Error.ToString() }; } SetNewsletterRead(); return new { Message = response, Type = MessageType.Success.ToString() }; }; }
public HomeModule() { Get["/"] = p => { var configuration = Nancy.TinyIoc.TinyIoCContainer.Current.Resolve <ServantConfiguration>(); var latestErrors = EventLogHelper.GetByDateTimeDescending(5).ToList(); latestErrors = EventLogHelper.AttachSite(latestErrors, Page.Sites); Model.UnhandledExceptions = latestErrors; Model.HaveSeenNewsletter = configuration.HaveSeenNewsletter; return(View["Index", Model]); }; Post["/subscribetonewsletter"] = p => { var email = Request.Form.Email; var firstname = Request.Form.Firstname; var lastname = Request.Form.Lastname; try { new System.Net.Mail.MailAddress(email); } catch { AddPropertyError("email", "Looks like the email is not valid."); } var serializer = new Nancy.Json.JavaScriptSerializer(); if (HasErrors) { return(new { Type = MessageType.Error.ToString(), Errors = serializer.Serialize(Model.Errors) });; } var response = MailchimpHelper.Subscribe(email, firstname, lastname); if (response != "true") { MailchimpResponse result = serializer.Deserialize <MailchimpResponse>(response); if (result.Code == 214) { SetNewsletterRead(); } if (result.Code == 502) { AddPropertyError("email", "Looks like the email is not valid."); return(new { Type = MessageType.Error, Errors = serializer.Serialize(Model.Errors) }); } return(new { Message = result.Error, Type = MessageType.Error.ToString() }); } SetNewsletterRead(); return(new { Message = response, Type = MessageType.Success.ToString() }); }; }
private static void refreshJSON() { Nancy.Json.JavaScriptSerializer js = new Nancy.Json.JavaScriptSerializer(); StatsJSON = js.Serialize(Stats); KeysJSON = js.Serialize(Stats.Keys); ActiveplayersJSON = js.Serialize(Stats.ActivePlayers); MemoryUsageJSON = js.Serialize(memoryUsage); RestartsJSON = js.Serialize(Stats.Restarts); }
public static void UpdateConfiguration(ServantConfiguration configuration) { var content = Serializer.Serialize(configuration); System.IO.File.WriteAllText(ConfigFilePath, content); TinyIoCContainer.Current.Register(configuration); }
private static Response ReturnJsonError(NancyContext context, Exception ex) { Response response = null; if (context.Request.Headers.Accept.Any(a => a.Item1.IndexOf("application/json", StringComparison.InvariantCultureIgnoreCase) >= 0)) { var serializer = new Nancy.Json.JavaScriptSerializer(); response = new Response() { StatusCode = HttpStatusCode.InternalServerError, ContentType = "application/json", Contents = s => { StreamWriter writer = new StreamWriter(s, new UTF8Encoding(false)) { AutoFlush = true }; // Don't dispose this! writer.Write(serializer.Serialize(new { detail = ex.Message })); } }; } try { StringBuilder log = GetLogForRequest(context); log.AppendLine().AppendLine("error: ").Append(ex.ToString()); TvLibrary.Log.Log.Error(log.ToString()); } catch { } return(response); }
public void Integer_dictionary_values_are_Json_serialized_as_integers() { dynamic value = 42; var input = new DynamicDictionaryValue(value); var sut = new Nancy.Json.JavaScriptSerializer(); var actual = sut.Serialize(input); actual.ShouldEqual(@"42"); }
public void String_dictionary_values_are_Json_serialized_as_strings() { dynamic value = "42"; var input = new DynamicDictionaryValue(value); var sut = new Nancy.Json.JavaScriptSerializer(); var actual = sut.Serialize(input); actual.ShouldEqual(@"""42"""); }