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