Reputation: 22578
I have an asp.net (get) web API that is being consumed using by both JSON and XML clients. Is there any type of attribute that I can decorate my code with that will change the various XML schema and JSON object names?
[WhatGoesHere("Record")]
public class AbcRecord
{
public bool IsVaid { get; set; }
[WhatGoesHere("Items")]
public IEnumerable<AbcItem> AbcItems { get; set; }
}
<Record xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MobilePortal.Library">
<IsVaid>true</IsVaid>
<Items>
...
</Items>
</Record>
Or the equivalent JSON
Upvotes: 0
Views: 903
Reputation: 45253
You can use this attribute on your class(es) to customize the namespace:
[DataContract(Namespace="http://example.com/namespace")]
I don't think any namespace is included in JSON by default so this attribute won't affect that.
Upvotes: 0
Reputation: 12395
Both the default XML and JSON formatters for Web API support DataContract and DataMember attributes. So your type would look like this:
[DataContract(Name = "Record")]
public class AbcRecord
{
[DataMember]
public bool IsVaid { get; set; }
[DataMember(Name = "Items")]
public IEnumerable<AbcItem> AbcItems { get; set; }
}
Upvotes: 3