public
Last active

ASP.NET WebApi OData $metadata

  • Download Gist
WebApiConfig.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Register an Action selector that can include template parameters in the name
config.Services.Replace(typeof(IHttpActionSelector), new ODataActionSelector());
 
var model = GetEdmModel();
var odataFormatter = new ODataMediaTypeFormatter(model);
config.Formatters.Clear();
config.Formatters.Insert(0, odataFormatter);
 
// Metadata routes to support $metadata and code generation in the WCF Data Service client.
config.Routes.MapHttpRoute(ODataRouteNames.Metadata, "api/$metadata", new { Controller = "ODataMetadata", Action = "GetMetadata" });
config.Routes.MapHttpRoute(ODataRouteNames.ServiceDocument, "api/", new { Controller = "ODataMetadata", Action = "GetServiceDocument" });
 
// Relationship routes (notice the parameters is {type}Id not id, this avoids colliding with GetById(id)).
config.Routes.MapHttpRoute(ODataRouteNames.PropertyNavigation, "api/{controller}({parentId})/{navigationProperty}");
 
// Route for manipulating links.
config.Routes.MapHttpRoute(ODataRouteNames.Link, "api/{controller}({id})/$links/{navigationProperty}");
 
// Routes for urls both producing and handling urls like ~/Product(1), ~/Products() and ~/Products
config.Routes.MapHttpRoute(ODataRouteNames.GetById, "api/{controller}({id})");
config.Routes.MapHttpRoute(ODataRouteNames.DefaultWithParentheses, "api/{controller}()");
config.Routes.MapHttpRoute(ODataRouteNames.Default, "api/{controller}");
}
 
private static IEdmModel GetEdmModel()
{
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<ProductionOrder>("ProductionOrders");
return modelBuilder.GetEdmModel();
}
}
jaydatafile.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*//////////////////////////////////////////////////////////////////////////////////////
////// Autogenerated by JaySvcUtil.exe http://JayData.org for more info /////////
////// oData V3 /////////
//////////////////////////////////////////////////////////////////////////////////////*/
(function(global, $data, undefined) {
 
$data.Entity.extend('Test.Testing.Models.ProductionOrder', {
'Id': { 'key':true,'type':'Edm.Int32','nullable':false,'required':true },
'OrderNr': { 'type':'Edm.Int32','nullable':false,'required':true },
'OrderLine': { 'type':'Edm.Int32','nullable':false,'required':true }
});
$data.EntityContext.extend('Default.Container', {
'ProductionOrders': { type: $data.EntitySet, elementType: Test.Testing.Models.ProductionOrder }
});
 
$data.generatedContexts = $data.generatedContexts || [];
$data.generatedContexts.push(Default.Container);
/*Context Instance*/
Default.context = new Default.Container( { name:'oData', oDataServiceHost: 'http://localhost:52132/api' });
 
})(window, $data);
metadata.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="1.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<Schema Namespace="VFP.Mes.Models" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="ProductionOrder">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="OrderNr" Type="Edm.Int32" Nullable="false" />
<Property Name="OrderLine" Type="Edm.Int32" Nullable="false" />
</EntityType>
</Schema>
<Schema Namespace="Default" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityContainer Name="Container">
<EntitySet Name="ProductionOrders" EntityType="VFP.Mes.Models.ProductionOrder" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.