Create OData V4 series services, odatav4

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.