如果我有这样的枚举:
[Flags]
public enum EyeColor {
All = 1,
Brown = 2,
Blue = 4,
Hazel = 8,
Green = 16
}
然后返回这样的JSON结果:
jsonBody.EyeColor = EyeColor.Brown | EyeColor.Blue;
我在客户端的jsonBody中看到以下内容:
"Brown, Blue"
但是,如果我通过EyeColor属性将上述字符串发送到WebAPI:
var eyeColor = "Brown, Blue"
var query = '?Index=1&EyeColor=` + eyeColor;
// send json GET request and use [FromUri] to extract
Serverside,我明白了:
dto.EyeColor: 0
虽然如果我这样做:
var eyeColor = "All"
var query = '?Index=1&EyeColor=` + eyeColor;
// send json GET request and use [FromUri] to extract
我得到了
dto.EyeColor: All
所以我想知道的是 - 如果我不仅要从WebApi检索字符串序列化的Enum,还要发送一个字符串来表示已选择的多个枚举(并反序列化为多个枚举),我需要做什么?
我的Global.asax:
JsonSerializerSettings jSettings = new JsonSerializerSettings();
jSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = jSettings;
GlobalConfiguration.Configuration
.Formatters
.JsonFormatter
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
答案 0 :(得分:3)
"Brown,+Blue"
是否需要这一切。