Skip to content

Instantly share code, notes, and snippets.

@raghuramn
raghuramn / ComplexCollection.cs
Created November 15, 2012 20:44
Complex Collections using ODataModelBuilder
using Microsoft.Data.Edm;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Formatter;
using System.Web.Http.SelfHost;
namespace ConsoleApplication1
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Formatter;
namespace Application
{
public class Program
@raghuramn
raghuramn / ODataProperty.cs
Created March 4, 2013 19:13
Sample showing how to implement ~/entityset/key/property kind of url support in web API OData. Run and try the url http://localhost:8080/customers(42)/ID or http://localhost:8080/customers(42)/Name for a demo.
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.ServiceModel;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
public class AsyncQueryableAttribute : ActionFilterAttribute
{
private static readonly MethodInfo _bufferAsync = typeof(AsyncQueryableAttribute).GetMethod("BufferAsyncCore", BindingFlags.NonPublic | BindingFlags.Static);
private readonly QueryableAttribute _innerFilter = new QueryableAttribute();
// this method is copied from ActionFilterAttribute
public async Task<HttpResponseMessage> ExecuteActionFilterAsync(
HttpActionContext actionContext,
CancellationToken cancellationToken,
Func<Task<HttpResponseMessage>> continuation)
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
namespace QueryTests
{
class Program
{
static void Main(string[] args)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using Microsoft.Data.Edm;
using Microsoft.Data.OData;
@raghuramn
raghuramn / DollarFormatHandler.cs
Created May 10, 2013 19:13
$format message handler for web API OData.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Hosting;
using System.Web.Http.OData;
public class DeeperNavigationsRoutingConvention : EntitySetRoutingConvention
{
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap)
{
if (odataPath.PathTemplate == "~/entityset/key/navigation/key") // ~/Customers(10)/Orders(42)
{
IEdmNavigationProperty navigationProperty = (odataPath.Segments[2] as NavigationPathSegment).NavigationProperty;
IEdmEntityType navigationPropertyType = navigationProperty.Type.AsCollection().ElementType().AsEntity().EntityDefinition(); // collection of entity.
controllerContext.RouteData.Values["key1"] = (odataPath.Segments[1] as KeyValuePathSegment).Value;
controllerContext.RouteData.Values["key2"] = (odataPath.Segments[3] as KeyValuePathSegment).Value;
public static IEdmModel GetEdmModel(this DbContext context)
{
using (MemoryStream stream = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(stream))
{
EdmxWriter.WriteEdmx(context, writer);
writer.Close();
stream.Seek(0, SeekOrigin.Begin);
using (XmlReader reader = XmlReader.Create(stream))
using Microsoft.Data.Edm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.OData;