【问题标题】:OData v4.0 - Change value to resultsOData v4.0 - 将值更改为结果
【发布时间】:2019-04-04 15:59:52
【问题描述】:

我目前正在编写 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;
        }

但是很遗憾,结果集还是一样的。

【问题讨论】:

    标签: c# api asp.net-core .net-core odata


    【解决方案1】:

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

       // 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);
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 2017-01-27
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2019-02-17
      • 2016-09-27
      相关资源
      最近更新 更多