Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration from RC2 to 1.0 : Returning 'dynamic' from controller action causes strange web api response

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": { }
  },
like image 413
Ruchan Avatar asked Dec 01 '25 04:12

Ruchan


1 Answers

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;
        }
    }
}
like image 63
Kiran Avatar answered Dec 02 '25 18:12

Kiran