Skip to content

Instantly share code, notes, and snippets.

@raghuramn
Created September 9, 2013 18:40
Show Gist options
  • Save raghuramn/6499741 to your computer and use it in GitHub Desktop.
Save raghuramn/6499741 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapODataRoute("odata", "", builder.GetEdmModel());
HttpServer server = new HttpServer(config);
HttpClient client = new HttpClient(server);
var response = client.GetAsync("http://localhost/Customers?$select=Name").Result;
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}
}
public class CustomersController : ODataController
{
public IEnumerable<Customer> Get(ODataQueryOptions<Customer> query)
{
Customer[] customers = new[] { new Customer { ID = 42, Name = "Raghu" } };
// Apply query
var result = customers;
// set the SelectExpandClause on the request to hint the odata formatter to
// select/expand only the fields mentioned in the SelectExpandClause.
if (query.SelectExpand != null)
{
Request.SetSelectExpandClause(query.SelectExpand.SelectExpandClause);
}
return result;
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment