简体   繁体   中英

OData v4.0 - Change value to results

I am currently writing an API but this time, the client-side is 3rd party. Luckily it's well documented, but I have 1 particular issue. The client expect the json like this:

{      "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       }}

I am using OData as I was required to use OData for the queryable options. However, my resultset is quite different:

{
    "@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       }}

The difference is

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

I asked the question at support, and they responded that I need to change value to results in order to make it work.. But how do I change this? I'm not entirely sure how to do this.

I've tried some dirty hack like this:

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

But unfortunately, the result set is still the same.

In order someone else will ever have to rename the value for some reason.. here is how I managed to do it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM