0

Im using this code(C#NETCORE5.0) to return a model:

        [HttpPost]
        [Authorize(AuthenticationSchemes = "Bearer")]
        public IActionResult GetTablero(GenericStringModel item){
            TableroVentasManager mng = new TableroVentasManager();
            TablaGeneralVentasModel response = mng.getTotalTable(item.zona);         
            return response != null ? Ok(response) : BadRequest();
        }

I want to reduce the size of the response (16.6mb actual) I have use

string ignored = JsonConvert.SerializeObject(response,
    Formatting.Indented,
    new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

but the size increase to 24mb

But if I only return the model, the size is 16.6MB theres a way to ignore null values to reduce the size?

Actual:

{
            "zona": "Z305",
            "companyId": "C018763",
            "formaEnvio": "CCI FORANEO",
            "mov": "salesorder",
            "tranId": 1193156,
            "subTotal": 164.8800,
            "consolidado": null,
            "nombre": "Ingresado",
            "fechaIngreso": "2022-12-02T10:39:56",
            "fechaLiberado": null,
            "fechaProceso": null,
            "fechaCancelado": null,
            "consolidado2": 0.0,
            "empacado": 0
        }

Desired:

 {
            "zona": "Z305",
            "companyId": "C018763",
            "formaEnvio": "CCI FORANEO",
            "mov": "salesorder",
            "tranId": 1193156,
            "subTotal": 164.8800,            
            "nombre": "Ingresado",
            "fechaIngreso": "2022-12-02T10:39:56",          
            "consolidado2": 0.0,
            "empacado": 0
        }
wozzarvl
  • 304
  • 4
  • 17

2 Answers2

0

If response is a List, can you perhaps use Linq to filter out null values:

response.RemoveAll(x => x == null)

Then return your Ok(response)

Nabeel A.
  • 13
  • 4
0

I think you can use a compression mechanism something like GZIP. Readmore ;

  1. Install the package:

    Microsoft.AspNet.WebApi.Extensions.Compression.Server

  2. Browse your solution to the App_Start folder then open the WebApiConfig.cs file, we will import two libs from the package we installed.

    using Microsoft.AspNet.WebApi.Extensions.Compression.Server;

    using System.Net.Http.Extensions.Compression.Core.Compressors;

  3. Finally at the end of the Register method the following line

    config.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

Before gzip application:

enter image description here

After gzip application:

enter image description here

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68