0

I'm facing little stranger issue with Web API controller. I have a collection which is being passed in an action of api controller. Object being used is collection is having 4 properties.

My action is able to accept collection parameter when it's properties are in specific order. See below :-

    [HttpPost]
    public ForexRates UpdateRates([FromBody] Rates rates)
    {
        // TODO: Obviously code :) 
        return rates;
    }

This code is being place in API controller & calling from Postman. See below:-

<rates>
  <rate> 
   <id>fefef</id>
    <rate>35353.333</rate>
    <series>dfefge</series>
    <series-order>sfefefef</series-order>  
</rate></rates>

If I change the order of the properties I started getting null value in my action. Can some one please explain this :)

Models

public class Rate
{
    public string Id { get; set; }
    public string Date { get; set; }
    public double Rate { get; set; }
}

public class Rates : Collection<ForexRate>
{
}
2
  • 1
    Please add the implementation of Rate and Rates Commented May 6, 2015 at 12:09
  • I have added models as well. Commented May 7, 2015 at 11:02

3 Answers 3

1

You will need to control the order with which your XML is serialized. Use XmlElementAttribute and specify the Order.

There is a similar question here

FYI, I suppose there is no way for you to change the order of the properties, while you supply from PostMan to your WebApi service. You will need to follow the exact order.

If you don't wanna do that, then pass this Xml as a string parameter and then parse it inside a method.

Sign up to request clarification or add additional context in comments.

Comments

0

The default binder can have issues when the same name is used in different places during binding.

In your case you've got Rate.Rate - both class name and property name. Try changing your class to (and corresponding xml for the post) :

public class Rate
{
    public string Id { get; set; }
    public string Date { get; set; }
    public double Value { get; set; }
}

and then try changing the order.

While I don't have a definitive reason why it works in one order and not another, it's likely that when it gets to the Rate(double) value it tries to create a new Rate(object) but doesn't have the correct properties (as its just a double).

A more complicated solution would be to write a specific model binder for the Rate object.

3 Comments

Not actually! The name of my class n property is not same. Models posted here are just dummy names of DTO classes.
Yes creating model binder is alternate solution but still before i try next, i would like to have reason of this situation.
I have created these DTO classes as Data Contracts. May be if that can help to understand .net default serialization behavior
0

The issue has to do with the DataContractSerializer which expects the elements to occur in a specific order (alphabetical with some consideration given to inheritance). That's the default serializer used when creating a Web API project.

You can override this and specify a different serializer during API Configuration like this:

GlobalConfiguration.Configuration.Formatters.XmlFormatter
  .SetSerializer<SomeType>(new XmlSerializer(typeof(SomeType)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.