Skip to content

Instantly share code, notes, and snippets.

@mstum
Last active December 24, 2015 21:09
Show Gist options
  • Save mstum/6863362 to your computer and use it in GitHub Desktop.
Save mstum/6863362 to your computer and use it in GitHub Desktop.
Microsoft.AspNet.WebApi.WebHost
Microsoft.AspNet.WebApi.OData
protected void Application_Start(object sender, EventArgs e)
{
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
public static void Register(HttpConfiguration config)
{
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Product>("Products");
Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "odata", model);
var queryAttribute = new QueryableAttribute()
{
AllowedQueryOptions = AllowedQueryOptions.All
};
config.EnableQuerySupport(queryAttribute);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
//config.EnableSystemDiagnosticsTracing();
}
public class ProductsController : EntitySetController<Product, int>
{
private static List<Product> products = new List<Product>()
{
new Product() {Id = 1, Name = "Hat", Price = 15, Category = "Apparel"},
new Product() {Id = 2, Name = "Socks", Price = 5, Category = "Apparel"},
new Product() {Id = 3, Name = "Scarf", Price = 12, Category = "Apparel"},
new Product() {Id = 4, Name = "Yo-yo", Price = 4.95M, Category = "Toys"},
new Product() {Id = 5, Name = "Puzzle", Price = 8, Category = "Toys"},
};
public override IQueryable<Product> Get()
{
return products.AsQueryable();
}
protected override Product GetEntityByKey(int key)
{
return products.Single(p => p.Id == key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment