0

When I access an ASP.NET web api using angular from the same web site, all the first letter lowercase, even if they aren't lowercase on the server.

However, if I move the API's to a different web site and enable CORS, I will receive the JSON exactly as the properties are written on the server.

Is there some way to control this difference? It becomes a mess when I need to move an API to a different web site

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Dennes Torres
  • 396
  • 2
  • 10

3 Answers3

0

The default for serializing output to JSON has changed so you may encounter this issue when migrating .Net frameworks. You can read more about the issue here or here but to resolve you can specify the DefaultContractResolver.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
}
McArthey
  • 1,614
  • 30
  • 62
0

In your Owin Startup add this line...

 public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var webApiConfiguration = ConfigureWebApi();            
        app.UseWebApi(webApiConfiguration);
    }

    private HttpConfiguration ConfigureWebApi()
    {
        var config = new HttpConfiguration();

        // ADD THIS LINE HERE AND DONE
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

        config.MapHttpAttributeRoutes();
        return config;
    }
}
santosh singh
  • 27,666
  • 26
  • 83
  • 129
0

JSON is case sensitive as a rule JSON keys should have matching case. Some unix servers will ignore case sensitivity but I believe windows servers enforce it.

The error is in the naming conventions in the api or code requesting/processing those keys. Best to use lowercase keys using a under_score as the seperator instead of camelCasing.

http://jsonrpc.org/historical/json-rpc-1-1-alt.html#service-procedure-and-parameter-names

ref: When is case sensitivity important in JSON requests to ASP.NET web services (ASMX)?

digital-pollution
  • 1,054
  • 7
  • 15