Skip to content

Instantly share code, notes, and snippets.

@lnickers2004
Last active January 3, 2016 12:29
Show Gist options
  • Save lnickers2004/8463611 to your computer and use it in GitHub Desktop.
Save lnickers2004/8463611 to your computer and use it in GitHub Desktop.
WebAPI 2: Foods ApiController which returns an IEnumerable type and behind-the-scenes depends upon the JSON.NET Camel Case Contract Resolver Formatter to format the JSON nicely for client consumption. NOTE: we still need to change this to use dependency Injection of an Entity Repository-- to make for better decopling and testing.
using CountingKs.Data;
using CountingKs.Data.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace CountingKs.Controllers
{
public class FoodsController : ApiController
{
public IEnumerable<Food> Get()
{
var repo = new CountingKsRepository(
new CountingKsContext());
var results = repo.GetAllFoods()
.OrderBy(f => f.Description)
.Take(25)
.ToList();
return results;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment