I've created an ASP.NET 2.0 webapi and am trying to return an abstract type from a method which returns IActionResult, i.e.
// GET api/trades/5
[HttpGet("{id}", Name = "GetTrade")]
[ProducesResponseType(typeof(Trade), 200)]
[ProducesResponseType(404)]
public IActionResult Get(int id)
{
var item = _context.Trades.FirstOrDefault(trade => trade.Id == id);
if (item == null)
{
return NotFound();
}
return Ok(item);
}
The Trade type is an abstract base class, I want the serialised JSON to include the $type attribute so the client can deserialise to the correct concrete type. The code below controls the output serialiser if I change the method to return Trade (the json returned contains a $type attribute with the concrete type name) but not IActionResult (no $type attribute).
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddDbContext<RiskSystemDbContext>(opt => opt.UseInMemoryDatabase("RiskSystemDb"));
services
.AddMvc(options => {})
.AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new StringEnumConverter());
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
});
}
How do I set TypeNameHandling for an IActionResult?
Edit:
For a class FutureTrade : Trade {} I expect
{
"$type": "RiskSystem.Model.FutureTrade, RiskSystem.Model",
"id": 1,
"createdDateTime": "2018-04-12T15:59:11.3680885+12:00"
...
}
Getting
{
"id": 1,
"createdDateTime": "2018-04-12T15:59:11.3680885+12:00"
...
}
The following works as expected
// GET api/trades
[HttpGet]
public IEnumerable<Trade> Get()
{
return _context.Trades.ToList();
}
Regards Dave
options.SerializerSettings.TypeNameHandling = TypeNameHandling.ObjectsTypeNameHandling.Noneso the $type is not included, you can change it as per your requirement.