Option 1 - Create a New Model
Instead of returning
public IEnumerable<Person> Get()
return
public People Get()
Where
public class People {
public IEnumerable<Person> People {get; set;}
}
Option 2 - return dynamic
Instead of returning
public IEnumerable<Person> Get()
return
public dynamic Get() {
IEnumerable<Person> p =
return new {people = p};
}
Option 3 - change JsonMediaTypeFormatter
You can still come back
public IEnumerable<Person> Get()
but add the following class:
public class PeopleAwareJsonMediaTypeFormatter : JsonMediaTypeFormatter
{
public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, HttpContent content, TransportContext transportContext)
{
if ((typeof (IEnumerable<People>).IsAssignableFrom(type)))
{
value = new {people = value};
}
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
}
now in WebApiConfig just register a new formatter instead of the old JSON one:
config.Formatters.RemoveAt(0);
config.Formatters.Insert(0, new PeopleAwareMediaTypeFormatter());
source
share