Uppercase attribute that converts the input to uppercase

前端 未结 4 1973
长发绾君心
长发绾君心 2020-12-18 01:36

I am working in MVC4 and want to define a model using an Uppercase attribute. The idea would be that the presence of the Uppercase attribute would cause the mod

4条回答
  •  不知归路
    2020-12-18 02:01

    For Web API purpose it is better to convert the incoming json to uppercase or lowercase.

        public class ToUpperCase : JsonConverter
        {
            public override bool CanConvert(Type objectType)
            {
                return objectType == typeof(string);
            }
    
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                return reader.Value.ToString().ToUpper();
            }            
        }
    
    
    
        [Display(Name = "PNR NAME")]
        [JsonConverter(typeof(Annotations.ToUpperCase))]
        public string PNR { get; set; }
    

    OR Globally;

      protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            //.......... others
    
    
    
            JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings();
            jSettings.Converters.Add(new UpperCaseStringConverter());
            jsonFormatter.SerializerSettings = jSettings;
        }
    

提交回复
热议问题