gpt4 book ai didi

c# - OData v4.0 - 将值更改为结果

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:46 25 4
gpt4 key购买 nike

我目前正在编写 API,但这一次,客户端是第 3 方。幸运的是,它有据可查,但我有 1 个特殊问题。客户期望这样的 json:

{      "results":[         {            "Id":"2C9E76EF-2532-4CA5-B4C6-03E39A31867A",          "Code":1,          "Name":"Optimizers B.V.",          "VatCode":"96541122",          "ChamberOfCommerceCode":"28084551",          "LanguageCode":"NL",          "Discount":2.35,          "CustomerManager":"Robin van Halen",          "PriceList":"7208812F-EEDA-4345-B3C6-1F1A960558C1",          "PaymentCondition":"7BA77586-3A3A-4D07-8DB8-2188F48BD68A",          "VatLiable":true       }}

我正在使用 OData,因为我需要将 OData 用于可查询选项。但是,我的结果集非常不同:

{
"@odata.context": "http://localhost:52973/odata/$metadata#Customers",
"value": [ { "Id":"2C9E76EF-2532-4CA5-B4C6-03E39A31867A", "Code":1, "Name":"Optimizers B.V.", "VatCode":"96541122", "ChamberOfCommerceCode":"28084551", "LanguageCode":"NL", "Discount":2.35, "CustomerManager":"Robin van Halen", "PriceList":"7208812F-EEDA-4345-B3C6-1F1A960558C1", "PaymentCondition":"7BA77586-3A3A-4D07-8DB8-2188F48BD68A", "VatLiable":true }}

区别在于

"@odata.context": "http://localhost:52973/odata/$metadata#Customers",
"value": [ ....

我在支持部门问了这个问题,他们回答说我需要将值更改为结果才能使其正常工作。但是我该如何更改呢?我不太确定该怎么做。

我试过这样一些肮脏的技巧:

 public IQueryable<Customer> Get()
{
var AllCusts = GetAllCustomers(); //this is of type IQueryable
//Test in order to change value to results

var x = JsonConvert.SerializeObject(AllCusts, Formatting.Indented);
var xyz = JsonConvert.DeserializeObject<List<Customer>>(x.Replace("value", "Results"));

var asQuery = xyz.AsQueryable();


return asQuery;
}

但不幸的是,结果集还是一样。

最佳答案

为了让其他人出于某种原因不得不重命名该值..这是我设法做到的:

   // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Add middleware before MVC
app.Use(async (ctx, next) =>
{
// Capture the original response body stream
var responseStream = ctx.Response.Body;

// Replace it with our own, so that we can read it
using (var bodyStream = new MemoryStream())
{
ctx.Response.Body = bodyStream;


// Run ASP.NET MVC (ie. get the results back from your code)
await next();

// Put the original response body stream back
ctx.Response.Body = responseStream;

// Read the one that we captured
bodyStream.Seek(0, SeekOrigin.Begin);
var responseBody = await new StreamReader(bodyStream).ReadToEndAsync();

// If it's ODATA & JSON & 200 (success), replace the "value" with "results"
if (ctx.Response.ContentType.Contains("application/json") && ctx.Response.ContentType.Contains("odata") && ctx.Response.StatusCode == 200)
{
responseBody = responseBody.Replace("\"value\"", "\"results\"");
}

// Write back the response body (whether modified or not) to the original response stream
await ctx.Response.WriteAsync(responseBody);
}
});

关于c# - OData v4.0 - 将值更改为结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520401/

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