;

WebAPI Error: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'?


Tutorialsrack 05/10/2020 C# ASP.NET WEB API

In this article, you will learn how to resolve ‘The 'ObjectContent`1' type failed to serialize the response body for content-type 'application/XML; charset=utf-8' ?’. I found two solutions to solve this problem.

The error you got by default when you serialize the response body:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

I read some articles on the web, I found the new error is as follow:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.

I Fixed these error by adding a few lines of code to the WebApiConfig.cs file, Here the solutions to resolve the errors:

Solution 1: Add these lines to the Application_Start() method of Global.asax file

Solution 1: Add these lines to the Application_Start() method of Global.asax file
//Add these Lines to Serializing Data to JSON Format
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

Complete Code of Global.asax File.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace CRUD_API
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //Add these Lines to Serializing Data to JSON Format
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
        }
    }
}

Solution 2: Add these lines to the Register() method of WebApiConfig.cs file inside the App_Start folder

Solution2: Add these lines to the Register() method of WebApiConfig.cs file inside the App_Start folder
// Serializing the Data to Json Format
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
Complete Code of WebApiConfig.cs file
using System.Web.Http;

namespace CRUD_API
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            
            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            // Serializing the Data to Json Format
            var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            config.Formatters.Remove(config.Formatters.XmlFormatter);
        }
    }
}

I hope this article will help you to understand how to resolve the 'ObjectContent`1' type failed to serialize the response body for content-type 'application/XML; charset=utf-8'?

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags