我正在探索OData。
我一直在关注Referbruv的Working with OData - Integrating an Existing ASP.NET Core 3.x API!
我的EdmModel遵循他的,如下所示:
internal static IEdmModel GetEdmModel() {
// create OData builder instance
var builder = new ODataConventionModelBuilder();
// CLIENTS
// map the entities set which are the types returned from the endpoint onto the OData pipeline
// the string parameter is the name of the controller
// which supplies the data of type Client entity model in this case
builder.EntitySet<ClientIndexDto>("ODataClient").EntityType.HasKey(x => x.id);
builder.EntitySet<ClientDetailsDto>("ODataClientDetails").EntityType.HasKey(x => x.id);
// configure a function onto the builder, AllClients
// which is same as the name provided in the ODataRoute
builder.Function("AllClients")
.ReturnsCollectionFromEntitySet<ClientIndexDto>("ODataClient");
builder.Function("ClientById")
.ReturnsCollectionFromEntitySet<ClientDetailsDto>("ODataClientDetails")
.Parameter<int>("id");
return builder.GetEdmModel();
}这一节:
builder.Function("ClientById")
.ReturnsCollectionFromEntitySet<ClientDetailsDto>("ODataClientDetails")
.Parameter<int>("id");处理通过Id获取客户端。
为了完整起见,我在我的StartUp.cs文件中包含了OData的以下内容:
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.Select().Filter().OrderBy().Expand().Count().MaxTop(10);
endpoints.MapODataRoute("odata", "odata", GetEdmModel());
});返回的$metadata如下:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="JobsLedger.MODELS.API.App.Client" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="ClientIndexDto">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Edm.Int32" Nullable="false" />
<Property Name="ClientNo" Type="Edm.String" />
<Property Name="Active" Type="Edm.Boolean" Nullable="false" />
<Property Name="ClientFirstName" Type="Edm.String" />
<Property Name="ClientLastName" Type="Edm.String" />
<Property Name="Company" Type="Edm.Boolean" Nullable="false" />
<Property Name="CompanyName" Type="Edm.String" />
<Property Name="MobilePhone" Type="Edm.String" />
<Property Name="IsWarrantyCompany" Type="Edm.Boolean" Nullable="false" />
<Property Name="JobsCount" Type="Edm.String" />
</EntityType>
<EntityType Name="ClientDetailsDto">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Edm.Int32" Nullable="false" />
<Property Name="ClientNo" Type="Edm.String" />
<Property Name="Company" Type="Edm.Boolean" Nullable="false" />
<Property Name="IsWarrantyCompany" Type="Edm.Boolean" Nullable="false" />
<Property Name="CompanyName" Type="Edm.String" />
<Property Name="ClientFirstName" Type="Edm.String" />
<Property Name="ClientLastName" Type="Edm.String" />
<Property Name="MobilePhone" Type="Edm.String" />
<Property Name="DeActivated" Type="Edm.String" />
<Property Name="CreatedOn" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOn" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
<Property Name="SuburbId" Type="Edm.Int32" Nullable="false" />
<NavigationProperty Name="Address" Type="JobsLedger.MODELS.Common.Address.AddressDto" />
<NavigationProperty Name="ClientJobs" Type="Collection(JobsLedger.MODELS.API.App.Job.ClientJobDto)" />
<NavigationProperty Name="ClientNotes" Type="Collection(JobsLedger.MODELS.Common.Notes.ClientNoteDto)" />
</EntityType>
</Schema>
<Schema Namespace="JobsLedger.MODELS.Common.Address" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="AddressDto">
<Key>
<PropertyRef Name="Address1" />
</Key>
<Property Name="Address1" Type="Edm.String" Nullable="false" />
<Property Name="Address2" Type="Edm.String" />
<Property Name="SuburbId" Type="Edm.Int32" Nullable="false" />
<Property Name="SuburbName" Type="Edm.String" />
<Property Name="StateShortName" Type="Edm.String" />
<Property Name="Postcode" Type="Edm.String" />
</EntityType>
</Schema>
<Schema Namespace="JobsLedger.MODELS.API.App.Job" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="ClientJobDto">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="JobNo" Type="Edm.String" />
<Property Name="AgentJobNo" Type="Edm.String" />
<Property Name="Type" Type="Edm.String" />
<Property Name="Status" Type="Edm.String" />
<Property Name="WarrantyCompany" Type="Edm.String" />
<Property Name="NumberOfVisits" Type="Edm.String" />
<Property Name="CreatedOn" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOn" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
<NavigationProperty Name="JobNotes" Type="Collection(JobsLedger.MODELS.Common.Notes.JobNoteDto)" />
<NavigationProperty Name="JobVisits" Type="Collection(JobsLedger.MODELS.API.App.Job.JobVisitDto)" />
</EntityType>
<EntityType Name="JobVisitDto">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="DateCreated" Type="Edm.String" />
<Property Name="VisitDate" Type="Edm.String" />
<Property Name="StartTime" Type="Edm.String" />
<Property Name="EndTime" Type="Edm.String" />
</EntityType>
</Schema>
<Schema Namespace="JobsLedger.MODELS.Common.Notes" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="JobNoteDto">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="JobId" Type="Edm.Int32" Nullable="false" />
<Property Name="Details" Type="Edm.String" />
<Property Name="NoteType" Type="Edm.String" />
<Property Name="CreatedOnDate" Type="Edm.String" />
<Property Name="CreatedOnTime" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOnDate" Type="Edm.String" />
<Property Name="ModifiedOnTime" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
</EntityType>
<EntityType Name="ClientNoteDto">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Edm.Int32" Nullable="false" />
<Property Name="Details" Type="Edm.String" />
<Property Name="NoteType" Type="Edm.String" />
<Property Name="CreatedOnDate" Type="Edm.String" />
<Property Name="CreatedOnTime" Type="Edm.String" />
<Property Name="CreatedBy" Type="Edm.String" />
<Property Name="ModifiedOnDate" Type="Edm.String" />
<Property Name="ModifiedOnTime" Type="Edm.String" />
<Property Name="ModifiedBy" Type="Edm.String" />
</EntityType>
</Schema>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Function Name="AllClients">
<ReturnType Type="Collection(JobsLedger.MODELS.API.App.Client.ClientIndexDto)" />
</Function>
<Function Name="ClientById">
<Parameter Name="id" Type="Edm.Int32" Nullable="false" />
<ReturnType Type="Collection(JobsLedger.MODELS.API.App.Client.ClientDetailsDto)" />
</Function>
<EntityContainer Name="Container">
<EntitySet Name="ODataClient" EntityType="JobsLedger.MODELS.API.App.Client.ClientIndexDto" />
<EntitySet Name="ODataClientDetails" EntityType="JobsLedger.MODELS.API.App.Client.ClientDetailsDto" />
<FunctionImport Name="AllClients" Function="Default.AllClients" EntitySet="ODataClient" IncludeInServiceDocument="true" />
<FunctionImport Name="ClientById" Function="Default.ClientById" EntitySet="ODataClientDetails" IncludeInServiceDocument="true" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>我的控制器如下:
using JobsLedger.API.Controllers.API.App.Interfaces;
using JobsLedger.API.ControllerServices.API.App.ClientService.Interfaces;
using JobsLedger.DATA;
using JobsLedger.DATA.Repositories.Interfaces;
using JobsLedger.MODELS.API.App.Client;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
namespace JobsLedger.API.Controllers.API.App {
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "TenantAdmin,Admin,Employee")]
public class ODataClientController : ODataController, IClientController {
private readonly IClientServices _clientServices;
public ODataClientController( IClientServices clientServices) {
_clientServices = clientServices;
}
[HttpGet]
[EnableQuery()]
[ODataRoute("AllClients()")]
public IActionResult Get()
{
return Ok(_clientServices.GetAllClientsAsDto());
}
[HttpGet]
[EnableQuery]
[ODataRoute("ClientById(id={id})")]
public IActionResult Get(int id) {
return Ok(_clientServices.GetClient(id));
}我想获得一个ID为"5“的客户端,所以我在Postman中使用了以下get命令:
https://localhost:44301/odata/ClientById(id=5)?正如教程中所指出的,这是用括号括起来的"id=5“
它到达端点,但id设置为"0“。

给定我的EdmModel、#元数据和针对单个Id的控制器操作,为什么当我使用(Id=5)发送GET命令时,我会得到Id=0?我需要做些什么来解决这个问题?
发布于 2020-05-10 16:04:41
在文章评论中,Referbruv还提到了.net核心3.1中的路由问题。所以,你可以在3.0中尝试它,如果它在那里工作,它就是3.1的问题。
如果你能用id=做到这一点,那么基于attr-routing,这应该是可行的。
[ODataRoute("ClientById({id})")]https://stackoverflow.com/questions/61708181
复制相似问题