Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to construct OData query Restful API for ASP.NET Core

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "ASP.NET Core how to build OData query Restful API", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "ASP.NET Core how to build OData query Restful API" bar!

Foreword:

This article uses the .NET Core SDK 3.1 version.

OData is the abbreviation of Open Data Protocol.

OData allows you to create and use queryable and interoperable Restful API in a simple and standard way.

Official documents visit OData-Basic Tutorial

Create a Web API project and introduce OData-related NuGet packages:

Dotnet pack Microsoft.AspNetCore.OData-v 7.5.8 I. Construction of OData entity model

Before building an OData entity model, you need to create the relevant DTO

Public class Person {public int Id {get; set;} public string Name {get; set;} public int Age {get; set;}}

Then construct the OData entity model

Public static class PersonModelBuilder {public static IEdmModel GetEdmModel () {var oDataBuilder = new ODataConventionModelBuilder (); oDataBuilder.EntitySet ("Person"); return oDataBuilder.GetEdmModel ();}} II. Configure OData middleware

Configure the OData service

Public void ConfigureServices (IServiceCollection services) {/ / Services.AddOData ();}

Configure OData middleware

Public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {app.UseRouting (); app.UseEndpoints (endpoints = > {endpoints.Select (). Filter (). OrderBy (). Count (). MaxTop (10) / / the route name of the first parameter, the route prefix of the second parameter, the method of creating the OData entity data model for the third parameter / / the route endpoints.MapODataRoute ("odata", "odata", PersonModelBuilder.GetEdmModel ()) associated with OData is created in this way;} third, OData implements the Restful API query

Create a PersonController controller and inherit ODataController.

Add [EnableQuery] to the feature of Action to indicate that the current API is a method of OData protocol

The feature [FromODataUri] on the entry of Action indicates that the current input parameter comes from the Uri in OData format.

Public class PersonController: ODataController {private static readonly List PEOPLE = new List {new Person {Id = 1, Name = "Zhang San", Age = 18}, new Person {Id = 2, Name = "Li Si", Age = 19}, new Person {Id = 3, Name = "Wang Wu", Age = 20}, new Person {Id = 4, Name = "Zhao Liu", Age = 21},} [HttpGet, EnableQuery] public ActionResult Get () {var people = PEOPLE; return Ok (people);} [HttpGet, EnableQuery] public IActionResult Get ([FromODataUri] int key) {var people = PEOPLE; return Ok (people.FirstOrDefault (b = > b.Id = = key);}}

Access the API that queries all the data through the following Uri

Http://localhost:5000/odata/person?$select=name,age&$orderby=age desc&$count=true&$top=2&$skip=2

Let's look at the results first, and we can get the following JSON

{"@ odata.context": "http://localhost:5000/odata/$metadata#Person(Name,Age)"," @ odata.count ": 4," value ": [{" Name ":" Li Si "," Age ": 19}, {" Name ":" Zhang San "," Age ": 18}]}

It is not difficult to see that the Uri of OData query API uses the parameters starting with the $symbol to modify the results returned by Action, sort, filter fields, and so on.

We visit the Value Uri of the @ odata.context parameter, and we can see the details of the relevant entities of the interface.

Use the following Uri to access the API that queries a piece of data

Http://localhost:5000/odata/person(1)

We can get the following JSON

{"@ odata.context": "http://localhost:5000/odata/$metadata#Person/$entity"," Id ": 1," Name ":" Zhang San "," Age ": 18} Thank you for reading. This is the content of" how ASP.NET Core constructs OData query Restful API ". After the study of this article, I believe you have a deeper understanding of how ASP.NET Core constructs OData query Restful API. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report