Microsoft is working on a new framework called Restier:
RESTier is a RESTful API development framework for building standardized, OData V4 based REST services on .NET. It can be seen as a middle-ware on top of Web API OData. RESTier combines the convenience and simplicity of WCF Data Services alongside the flexibility and extensibility of Web API OData.
Similar to a WCF Data Service it exposes your Data Model through a REST service.
Get started.
- Create a new MVC application
- Add an Entity Framework Code First context
- Include the Microsoft.Restier NuGet Package(don’t forget to set Include prerelease)
- Create a Domain class that wraps your DbContext:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Restier.EntityFramework; | |
public class NorthwindDomain : DbDomain<NorthwindContext> | |
{ | |
public NorthwindContext Context | |
{ | |
get { return DbContext; } | |
} | |
} |
- Add a Web Api controller inheriting from ODataDomainController that wraps the Domain class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Restier.WebApi; | |
public class NorthwindController : ODataDomainController<NorthwindDomain> | |
{ | |
private NorthwindContext DbContext | |
{ | |
get { return Domain.Context;} | |
} | |
} |
- As a last step add an extra route to the WebApiConfig file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Restier.WebApi.Batch; | |
using RestierSample.Controllers; | |
using Microsoft.Restier.WebApi; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Http; | |
namespace RestierSample.App_Start | |
{ | |
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
config.MapHttpAttributeRoutes(); | |
config.MapODataDomainRoute<NorthwindController>( | |
"NorthwindApi", "api/Northwind", | |
new ODataDomainBatchHandler(GlobalConfiguration.DefaultServer)); | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
} | |
} | |
} |
- If you run the application and browse to /api/northwind, you can see the available entity sets: