I have a class:
[JsonObject(MemberSerialization.OptIn)] public class UserPreferenceDTO { [JsonProperty] public string Name { get; set; } [JsonProperty] public string Value { get; set; } public static explicit operator UserPreferenceDTO(UserPreference pref) { if (pref == null) return null; return new UserPreferenceDTO { Name = pref.Name, Value = pref.Value }; } public static explicit operator UserPreference(UserPreferenceDTO pref) { if (pref == null) return null; return new UserPreference { Name = pref.Name, Value = pref.Value }; } }
and controller, for example:
public HttpResponseMessage Post(int caseid, Guid id, UserPreferenceDTO prefs) { ... }
NOTE : the controller class is decorated with the [CamelCaseControllerConfig] attribute that does this:
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { var formatter = controllerSettings.Formatters.OfType<JsonMediaTypeFormatter>().Single(); controllerSettings.Formatters.Remove(formatter); formatter = new JsonMediaTypeFormatter { SerializerSettings = { ContractResolver = new CamelCasePropertyNamesContractResolver() } }; controllerSettings.Formatters.Add(formatter); }
On the client, I send the object as follows:
{ name: "name", value: "Some value" }
Often value is JS boolean. The problem is that when it reaches the controller, the boolean value is converted to C # boolean ( True / False ) and gated.
For example, sending
'{ "name": "wantColFit", "value": "false" }'
becomes:

in the .NET controller.
If you look at the definition of the model ( UserPreferenceDTO ) value , take string . So why does a serializer convert a value to a boolean?
I would prefer to keep this value as "true" / "false" when it is saved (which will simplify the analysis of the logical value on the client, since JSON.parse("true") === true , but JSON.parse("True") !== true )
seebiscuit
source share