Wrong Serialization on LINQ Web API

web api json serializer settings
web api xml serialization
web api uses which of the following open-source library for json serialization
serialization and deserialization in web api
c# serialize object
jsonnetformatter
web api controller must be derived from
which of the following property returns all the formatters in web api?

I have a LINQ query as that:

public List<tblStudent> GetNames()
{
    var result = (from student in db.tblStudents.ToList()
                  select new tblStudent 
                  {
                      StudentID = student.StudentID,
                      StudentName = student.StudentName,
                      Email = student.Email,
                      IsDeleted= student.IsDeleted

                      //tblDepartment = department.tblStudents
                  });
    return result.ToList();
}

When I executed that query it gave me a result as that:

But I don't want to see null values in my result. How I can solve that problem?

While I haven't tried it, I'd expect you to be able to configure Json.NET to ignore null values when serializing. Within ConfigureServices, write this:

services.AddJsonFormatters(settings => settings.NullValueHandling = NullValueHandling.Ignore);

Using an anonymous type instead would work too, but that becomes trickier if you have a list where some objects have non-null values for certain properties, and other objects have null values. It's good to be aware of both approaches, so you can use whichever one is more appropriate in any situation.

Error with WebApi serialization, The Inner Exception message is: Type 'System.Linq.Enumerable+ WhereSelectListIterator 2[[EPiServer.Find.Api.SearchHit 1[[DGTNext.Api.Data. Serialization and Model Binding in ASP.NET Web API. 01/17/2012; 2 minutes to read +1; In this article. How ASP.NET Web API binds HTTP messages to domain models, and how to work with media formats in Web API.

You can use an anonymous type to return only the desired members when returning the collection from the ApiController

For example

public class MyApiController : ApiController {

    //...

    public IHttpActionResult MyControllerAction() {
        var result = myService.GetNames()
                    .Select(student => select new { //<-- Note what was done here
                          StudentID = student.StudentID,
                          StudentName = student.StudentName,
                          Email = student.Email,
                          IsDeleted= student.IsDeleted
                      });
        return Ok(result.ToList());
    }
}

LINQ to SQL and Serialization, LINQ to SQL provides entity objects that can be used easily for object access to data members. if you need to serialize them in Web Services or in other ways because NET and Java APIs for file formats – natively work with DOCX, XLSX, You can also set the ChildProperty option to false altogether to� The default serializer in ASP.NET Web API (at least in Beta) is the DataContractJsonSerializer with all of its warts and inability to not serializer non-typed objects. In this post I'll talk about the issues and how to plug-in alternate JSON parsers to handle more complete JSON serialization in Web API.

I just added that line on my WebApiConfig.cs and my problem been solved.

 config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

Using an alternate JSON Serializer in ASP.NET Web API, NET Web API (at least in Beta) is the DataContractJsonSerializer with all parsers to handle more complete JSON serialization in Web API. messages seems like overkill and projections using Linq makes this much easier to code up . type == typeof(JsonObject) || type== typeof(JsonArray) ) return false;� For example, Entity Framework api implements IQueryable<T> interface to support LINQ queries with underlaying databases such as MS SQL Server. Also, there are APIs available to access third party data; for example, LINQ to Amazon provides the ability to use LINQ with Amazon web services to search for books and other items.

JSON and XML Serialization in ASP.NET Web API, Web API provides media-type formatters for both JSON and XML. you can deserialize the request body to a Newtonsoft.Json.Linq.JObject type. translates to a status code 500 (Internal Server Error) response to the client. API Platform will always use the most specific definition. For instance, if normalization groups are set both at the resource level and at the operation level, the configuration set at the operation level will be used and the resource level ignored. In the following example we use different serialization groups for the GET and PUT operations:

Web API Error: The 'ObjectContent`1' type failed to serialize the , Mar 22, 2012 | .net asp.net web api c# entity framework error – “A circular reference was detected while serializing an object of type (…) discussion, we could use data projection to an anonymous type in the LINQ query and serialize that. Web API uses JSON.net natively, so we can take advantage of the JSON.net converters. One such converter is StringEnumConverter , which happens to do exactly what we want; namely, take an enumeration and serialize it to its name, not its value.

Serializing Entity Framework objects to JSON in ASP.NET Web API , NET Core 3.0 Preview 7 Asp.NET Web Apis, when returning a I think it makes sense to support Json serialization which Dictionary has GenericTypeArguments[0] == typeof(string)) return false; return typeToConvert. Serialize.Linq. Serialize.Linq is a c# library for serializing linq expressions. Formats it supports (but it is not limited to) are JSON, XML and BINARY.

Comments
  • It threw an error as that: The name 'Ok' does not exist in the current context
  • @HalilŞahin I assumed you were making the call within a ApiController as you mentioned Web API based on the tags of the question. To what type does GetNames() belong?
  • its an ApiController
  • @HalilŞahin then the method should exist docs.microsoft.com/en-us/dotnet/api/…
  • @HalilŞahin just to make sure the correct version is being used, is the ApiController in an attribute [ApiController] as in asp.net-core, or is it used as a base class like MyController: ApiController as in asp.net-web-api-2+