Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OData $select and $expand not working on XML response? #316

Open
Ken-ISC opened this issue Jun 17, 2022 · 0 comments
Open

OData $select and $expand not working on XML response? #316

Ken-ISC opened this issue Jun 17, 2022 · 0 comments

Comments

@Ken-ISC
Copy link

Ken-ISC commented Jun 17, 2022

I have been developing an API with XML and JSON responses integrating OData, but I'm having a problem retrieving XML responses with OData $select and $expand methods, but other methods work fine like $top, $filter and $orderby etc... In JSON response everything works fine with OData methods.

This is the response Error

System.Runtime.Serialization.InvalidDataContractException: Type 'Microsoft.AspNetCore.OData.Query.Wrapper.SelectSome`1[Test_API.v2.Models.User]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.

This is my Program.cs

builder.Services.AddMvc(config =>
{
config.RespectBrowserAcceptHeader = true;
config.ReturnHttpNotAcceptable = true;
config.InputFormatters.Add(new XmlDataContractSerializerInputFormatter(config));
config.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
config.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml"));
})
.AddOData(options => options.Select().Filter().OrderBy().Expand().OrderBy().Count().SetMaxTop(null));

This is my User.cs class

// This file has been auto generated by EF Core Power Tools.
#nullable disable
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Test_API.v2.Models
{
public partial class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual UserProfile Profile { get; set; }
}
}

This is my UserProfile.cs class

// This file has been auto generated by EF Core Power Tools.
#nullable disable
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Test_API.v2.Models
{
public partial class UserProfile
{
public UserProfile()
{
Users = new HashSet();
}
public int ProfileId { get; set; }
public string ProfileName { get; set; }
public string Email { get; set; }
public virtual ICollection Users { get; set; }
}
}

This is my Controller

[HttpGet]
[EnableQuery]
[ProducesResponseType(typeof(IUser), 200)]
public IQueryable GetUser()
{
return _context.Users.AsQueryable();
}

hoping anyone could help, been stuck in this for a month also I have been posting this on different community-based space websites but apparently, no one could help me find the solution.

please I really need a help

@Ken-ISC Ken-ISC closed this as completed Aug 18, 2022
@Ken-ISC Ken-ISC reopened this Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant