gpt4 book ai didi

c# - 如何在 POST/PUT OData 请求上扩展导航属性

转载 作者:行者123 更新时间:2023-12-05 06:12:14 25 4
gpt4 key购买 nike

我正在使用 OData ASP.NET Core 7.4.1

例如,请求 GET https://localhost:44346/api/v1/estates/74EEAB44-B8E7-EA11-B361-D43B04C15376?$expand=tenants ,我可以得到一个包含租户

信息的实体
{
"@odata.context": "https://localhost:44346/odata/v1/$metadata#Estates(tenants(),tenants())",
"value": [
{
"updatedOn": "2020-08-26T16:22:01.03+02:00",
"tenants": [
{
"name": "tenant"
}
]
}
]
}

但是使用 Put 请求 PUT https://localhost:44346/odata/v1/estates(74EEAB44-B8E7-EA11-B361-D43B04C15376)?$expand=tenants ,我可以得到我的返回实体但没有租户

{
"@odata.context": "https://localhost:44346/odata/v1/$metadata#Estates(tenants(),tenants())",
"value": [
{
"updatedOn": "2020-08-26T16:22:01.03+02:00",
"tenants": []
}
]
}

问题:如何在 PUT 请求中返回返回实体的 tenants,因为我不想做 GET(id) 调用服务器以刷新创建/更新的实体。服务器应返回与 GET(id) 完全相同的实体表示。

有一个ticket打开了这个但没有解决。有人解决了这个问题吗?

最佳答案

这里是Startup.cs中的设置

        app.UseMvc(routeBuilder =>
{
routeBuilder.EnableDependencyInjection();
routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(10).Count();

var builder = new ODataConventionModelBuilder(app.ApplicationServices);
builder.EntitySet<Tenants>("estates");
routeBuilder.MapODataServiceRoute("ODataRoute", "odata/v1", builder.GetEdmModel());
});

模式代码

public class Tenants
{
public int id { get; set; }
public DateTime updatedOn { get; set; }
public List<Tenant> tenants { get; set; }
}

public class Tenant
{
public string name { get; set; }
}

HTTP 获取

    [HttpGet]
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public async Task<IActionResult> Get()
{
var tenants = (await dataProvider.GetTenants()).ToList();
return Ok(tenants);
}

enter image description here

HTTP 放置

    [HttpPut("{key}")]
[ODataRoute("{key}")]
[EnableQuery]
public async Task<IActionResult> ([FromODataUri] Guid key, [FromBody] Estate estate)
{
var tenants = (await dataProvider.SetTenants(name)).ToList();
return Ok(tenants);
}

enter image description here

关于c# - 如何在 POST/PUT OData 请求上扩展导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63673270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com