Create OData V4 series services, odatav4
OData learning directory
Create an application
Add reference
Install-package entityframework, Install-Package Microsoft. AspNet. Odata, Install-Package Jquery
Add an object and generate data through EntityFramework
Create two classes in the Controller Folder: ProductsController and SuppliersController. All classes inherit from ODataController and configure routes in the WebApiConfig class.
Public static void Register (HttpConfiguration config) {// Web API configuration and service // Web API route config. mapHttpAttributeRoutes (); config. routes. mapHttpRoute (name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {id = RouteParameter. optional}); // build the routing service var route = config. mapODataServiceRoute ("odata", "Odata", model: GetModel (); // The second parameter Odata is prefix} public static Microsoft. OData. edm. IEdmModel GetModel () {var builder = new ODataConventionModelBuilder (); builder. entitySet <Product> ("Products"); // The second parameter Products corresponds to ProductsController builder. entitySet <Supplier> ("Suppliers"); return builder. getEdmModel ();}
After the route is configured, F5. If the following interface is displayed, the configuration is successful.
Build query service
[EnableQuery] public IQueryable<Product> Get() { return _dbContext.Products; } [EnableQuery] public SingleResult<Product> Get([FromODataUri] int key) { IQueryable<Product> result = _dbContext.Products.Where(p => p.Id == key); return SingleResult.Create(result); }
Obtain all product information
Get the object with product 1
Obtain the vendor whose product is 1