protected void Application_Start() { GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); BindingConfig.RegisterBindings(GlobalConfiguration.Configuration); GlobalConfiguration.Configure(WebApiConfig.Register); }
private object StringToType(string stringValue) { if (stringValue == null) { return(null); } var type = BindingConfig.GetType(Descriptor); if (type == typeof(string)) { return(stringValue); } if (type == typeof(int)) { int value; if (int.TryParse(stringValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out value) == true) { return(value); } else { return(null); } } if (type == typeof(Int32)) { Int32 value; if (Int32.TryParse(stringValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out value) == true) { return(value); } else { return(null); } } if (type == typeof(Int64)) { Int64 value; if (Int64.TryParse(stringValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out value) == true) { return(value); } else { return(null); } } if (type == typeof(decimal)) { decimal value; if (decimal.TryParse(stringValue, NumberStyles.Number, CultureInfo.InvariantCulture, out value) == true) { return(value); } else { return(null); } } if (type == typeof(double)) { double value; if (double.TryParse(stringValue, NumberStyles.Number | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out value) == true) { return(value); } else { return(null); } } if (type == typeof(DateTime)) { DateTime value; if (DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out value) == true) { return(value); } else { return(null); } } if (type == typeof(Guid)) { Guid value; if (Guid.TryParse(stringValue, out value) == true) { return(value); } else { return(null); } } if (type == typeof(bool)) { bool value; if (bool.TryParse(stringValue, out value) == true) { return(value); } else if (stringValue == "true" || stringValue == "on" || stringValue == "1") { return(true); } else { return(null); } } if (type.BaseType == typeof(Enum)) { try { var converter = TypeDescriptor.GetConverter(type); if (converter != null) { return((Enum)converter.ConvertFromString(stringValue)); } } catch (Exception ex) { } } return(null); }