I recently migrated from RC2 to 1.0 and i have issues on web api response.
For Action
public dynamic GetCountries(string startsWith)
{
return Ok(_countryService.GetList(startsWith));
}
Json Response In RC2
[{
"CountryCode": "ANG",
"CountryName": "Angola"
},
{
"CountryCode": "ANT",
"CountryName": "Antigua and Barbuda"
}]
Json Response in 1.0
{
"value": [
{
"countryCode": "ANG",
"countryName": "Angola"
},
{
"countryCode": "ANT",
"countryName": "Antigua and Barbuda"
}],
"formatters": [],
"contentTypes": [],
"declaredType": null,
"statusCode": 200
}
The response in 1.0 for dynamic type has added object wrapper which was not the case before with the properties being lower camel cased. Is there some thing i did wrong while migration?
Project.json
"dependencies": {
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview1-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.IdentityModel.Tokens": "5.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"net461": { }
},
This is a known issue: https://github.com/aspnet/Mvc/issues/4960
Why do you want the return type to be dynamic here? Why not return IActionResult instead?
Suggested workaround from the above issue or you could just change your return type to be IActionResult
public class Fix4960ActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context) { }
public void OnActionExecuted(ActionExecutedContext context)
{
var objectResult = context.Result as ObjectResult;
if (objectResult?.Value is IActionResult)
{
context.Result = (IActionResult)objectResult.Value;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With