Skip to content

Instantly share code, notes, and snippets.

@raghuramn
Last active December 18, 2015 13:09
Show Gist options
  • Save raghuramn/5788268 to your computer and use it in GitHub Desktop.
Save raghuramn/5788268 to your computer and use it in GitHub Desktop.
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;
return "Get" + navigationPropertyType.Name; // Maps to controller action Order GetOrder([FromODataUri]int key1, [FromODataUri]int key2)
}
return null;
}
}
class Program
{
static void Main(string[] args)
{
// Add the custom routing convention to the default routing conventions.
IList<IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault();
routingConventions.Insert(0, new DeeperNavigationsRoutingConvention());
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
builder.EntitySet<Order>("Orders");
HttpConfiguration config = new HttpConfiguration();
// map odata route with the custom routing convention included.
config.Routes.MapODataRoute("OData", "odata", builder.GetEdmModel(), new DefaultODataPathHandler(), routingConventions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment