WebAPI JSON Deserialization with property of Object

ontodaly

New Member
I have the following problem with the Asp.net Web Api. I try to use the following object as parameter of my action\[code\] [DataContract] public class MyObject { [DataMember] public object MyIdProperty {get;set;} }\[/code\]the property MyIdProperty can contains either a Guid or an Int32In MVC I did an ModelBinder and it works like a charm so I did one for the WebApi like this\[code\]public class HttpObjectIdPropertyModelBinder : IModelBinder{ public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType != ObjectType || (bindingContext.ModelName.TrimHasValue() && !bindingContext.ModelName.EndsWith("Id", StringComparison.OrdinalIgnoreCase))) { return false; } ValueProviderResult result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (result == null || result.RawValue =http://stackoverflow.com/questions/15709830/= null || result.RawValue.GetType() == ObjectType) { bindingContext.Model = null; return true; } bindingContext.ModelState.SetModelValue(bindingContext.ModelName, result); string stringValue = result.RawValue as string; if (stringValue == null) { string[] stringValues = result.RawValue as string[]; if (stringValues != null && stringValues.Length == 1) { stringValue = stringValues[0]; } if (stringValue == null) { return false; } } Guid guid; int integer; if (Guid.TryParse(stringValue, out guid)) { bindingContext.Model = guid; } else if (int.TryParse(stringValue, out integer)) { bindingContext.Model = integer; } else { return false; } return true; } private static readonly Type ObjectType = typeof(object); private static HttpParameterBinding EvaluateRule(HttpObjectIdPropertyModelBinder binder, HttpParameterDescriptor parameter) { if (parameter.ParameterType == ObjectType && parameter.ParameterName.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) { return parameter.BindWithModelBinding(binder); } return null; } public static void Register() { var binder = new HttpObjectIdPropertyModelBinder(); GlobalConfiguration.Configuration.Services.Insert(typeof(ModelBinderProvider), 0, new SimpleModelBinderProvider(typeof(object), binder)); GlobalConfiguration.Configuration.ParameterBindingRules.Insert(0, param => EvaluateRule(binder, param)); }}\[/code\]It's the first time I'm doing a model binder for the WebApi so I'm not even sure if I'm doing it well and if it's a good way to fix this problem.Anyway with this model binder if I have an action like this\[code\] public IEnumerable<MyObject> Get(object id) { // code here... }\[/code\]The parameter id is deserialized properly using the Json formatter or the Xml formatter and the model binderBut if I use the following action \[code\] public void Post(MyObject myObject) { // code here... }\[/code\]The parameter myObject is deserialized perfectly when I use the Xml formatter but when I use the Json formatter the property MyIdProperty contains a string instead of a Guid or Int32.And in both case my model binder is NOT used at all. It's like it stop the evaluation of the model at the action paramaters compare to MVC that use the model binders for each property with a complex type.Note: I would like to not use the true type or use a internal or protected property with the true type because I have this kind of property in a lot of different of object and the code will become really hard to maintain if I have to duplicate them each time
 
Back
Top