-1

I am writing a WebAPICore to return the JSON objects from the database. For unknown reason, the properties are returned as camelCase by default.

I have checked the SQL Script and it does return the correct case for the DataFields. But when I consume the service, the properties of the objects are changed to camelCase automatically.

For example, OfferingID is returned as offeringID

The existing Return JSON object

  {
    "offeringID": 120842,
    "courseCode": "FLTE2A1F/1",
    "courseName": "FLT -  E2 Certificate in Skills for Working Life (Animals) (QCF)"
  }

The format which I want to return

      {
        "OfferingID": 120842,
        "CourseCode": "FLTE2A1F/1",
        "CourseName": "FLT -  E2 Certificate in Skills for Working Life (Animals) (QCF)"
      }

The Model - Offering:

public class Offering
    {
        [Key]
        public int OfferingID { get; set; }
        public string CourseCode { get; set; }
        public string CourseName { get; set; }
    }

My WebAPI Controller Get Method

        [HttpGet("{id}")]
        public async Task<IActionResult> GetOfferingDetail(int id)
        {
            var obj = await _context.Offerings.FromSql("dbo.GetOfferingDetail @p0", id).SingleOrDefaultAsync();

            if (obj == null)
                return NotFound("ID not found");

            return new ObjectResult(obj);
        }

Configure Services Method in Startup.cs:

public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<DbContexts.OakCommonsDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyConnection")));
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                        .AllowAnyMethod()
                                                                         .AllowAnyHeader()));

            var mvccore = services.AddMvc();
            mvccore.AddJsonOptions(o => o.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore);            
        }

Could you please advise me how I could return JSON Objects in the Exact Case as I defined in the Model?

Tseng
  • 61,549
  • 15
  • 193
  • 205
TTCG
  • 8,805
  • 31
  • 93
  • 141
  • 1
    http://www.newtonsoft.com/json/help/html/contractresolver.htm – Amit Kumar Ghosh Aug 22 '16 at 10:31
  • Are you using `services.AddMvc().AddJsonOptions(opts =>{ opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();})` in `ConfigureServices` method ? – adem caglin Aug 22 '16 at 10:33
  • Possible duplicate of [asp.net core 1.0 web api use camelcase](http://stackoverflow.com/questions/38139607/asp-net-core-1-0-web-api-use-camelcase) – Set Aug 22 '16 at 10:39
  • I have updated the code to include ConfigureServices method. – TTCG Aug 22 '16 at 10:40
  • Which library do I need to get access to new CamelCasePropertyNamesContractResolver() ? – TTCG Aug 22 '16 at 10:41
  • You don't need any library, Default option should be same you except. I suspect your configuration might override `SerializerSettings.ContractResolver` option. – adem caglin Aug 22 '16 at 10:50

1 Answers1

0

Here is the working code. By default, WebAPI Core is going to use CamelCasePropertyNamesContractResolver(). You need to change it to DefaultContractResolver to render as you defined in the Model.

And DefaultContractResolver is under Newtonsoft.Json.Serialization namespace.

services.AddMvc()
    .AddJsonOptions(o => o.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)
    .AddJsonOptions(o => o.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());    
TTCG
  • 8,805
  • 31
  • 93
  • 141