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

前端 未结 8 1616
天命终不由人
天命终不由人 2020-11-27 16:56

Simplest example of this, I get a collection and try to output it via Web API:

// GET api/items
public IEnumerable Get()
{
    return MyContext.I         


        
相关标签:
8条回答
  • 2020-11-27 17:24

    if you have navigation properties and you do not want make them non virtual, you should using JSON.NET and change configuration in App_Start to using JSON not XML!
    after install JSON.NET From NuGet, insert this code in WebApiConfig.cs in Register method

    var json = config.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
    config.Formatters.Remove(config.Formatters.XmlFormatter);
    
    0 讨论(0)
  • 2020-11-27 17:25

    This helped me:
    Add the following code in Application_Start function of Global.asax.cs

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
        .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    GlobalConfiguration.Configuration.Formatters
        .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
    
    0 讨论(0)
  • 2020-11-27 17:29

    I just disabled proxy classes on a per needed basis:

        // GET: ALL Employee
        public IEnumerable<DimEmployee> Get()
        {
            using (AdventureWorks_MBDEV_DW2008Entities entities = new AdventureWorks_MBDEV_DW2008Entities())
            {
                entities.Configuration.ProxyCreationEnabled = false;
                return entities.DimEmployees.ToList();
            }
        }
    
    0 讨论(0)
  • 2020-11-27 17:32

    I would suggest Disable Proxy Creation only in the place where you don't need or is causing you trouble. You don't have to disable it globally you can just disable the current DB context via code...

        [HttpGet]
        [WithDbContextApi]
        public HttpResponseMessage Get(int take = 10, int skip = 0)
        {
            CurrentDbContext.Configuration.ProxyCreationEnabled = false;
    
            var lista = CurrentDbContext.PaymentTypes
                .OrderByDescending(x => x.Id)
                .Skip(skip)
                .Take(take)
                .ToList();
    
            var count = CurrentDbContext.PaymentTypes.Count();
    
            return Request.CreateResponse(HttpStatusCode.OK, new { PaymentTypes = lista, TotalCount = count });
        }
    

    Here I only disabled the ProxyCreation in this method, because for every request there is a new DBContext created and therefore I only disabled the ProxyCreation for this case . Hope it helps

    0 讨论(0)
  • 2020-11-27 17:32

    After disable Proxy Creation, use eager loading (Include()) to load the proxy object.

    0 讨论(0)
  • 2020-11-27 17:32

    In my Project EntityCollection returned from the WebApi action method. Configuration.ProxyCreationEnabled = false not applicable. I have tried the below approach it is working fine for me.

    1. Control Panel. 2.Turn on Windows Features on or off
    2. Choose Internet Information Service
    3. Check all the World Wide Web Components it would be better to check all the components in IIS.
    4. Install the components.
    5. Go to (IIS) type inetmgr in command prompt.
    6. select the published code in the Virtual directory.
    7. Convert into application
    8. Browse it the application.
    0 讨论(0)
提交回复
热议问题