How to return indented json content from an OData controller in asp core web api?

asp.net-core asp.net-core-2.1 asp.net-core-webapi entity-framework-core odata

Question

I can retrieve intended json result from normal WebApi using following way.

  services.AddMvc()
         .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
         .AddJsonOptions(x=>
         {
             x.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
         });

But I cannot find a way to output json like this when using ODataController as opposed to ControllerBase when web api is used. ODataController always sends a minified json.

public class EmployeeController : ODataController
{

    [EnableQuery()]
    public IActionResult Get()
    {
        return Ok(new BOContext().Employees.ToList());
    }
}

Also, startup.cs

 public class Startup
    {
        private static IEdmModel GetModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Employee>("Employee");
            return builder.GetEdmModel();
        }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddOData();
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });



            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddJsonOptions(x=>
                {
                    x.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.None;
                });
        }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapODataServiceRoute("odata", "odata", GetModel());
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

            });
        }
    }

The route is working and I am receiving correct data.

Is there a way to control and output indented json from an OData controller?

1
1
12/29/2018 1:41:48 PM

Popular Answer

I suggest you to make the transfer with minified jsonm, but use json beutifier to show formatted json. dont do this on the data flow phase.

If you are using javascript on the front-end side. You can simple use

JSON.stringify(jsObj, null, "\t"); // stringify with tabs inserted at each level
JSON.stringify(jsObj, null, 2);    // stringify with 2 spaces at each level
1
12/29/2018 5:43:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow