SingleResult <T> not serialized in web API when requesting a key

Trying to find a single record using the primary key CourseID vs odata web.api using this:

var editedcourse = container.Courses.Where(c => c.CourseID == ID).SingleOrDefault();

This is mistake:

    <m:innererror>
    <m:message>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/atom+xml; charset=utf-8'.</m:message>
    <m:type>System.InvalidOperationException</m:type>
    <m:stacktrace></m:stacktrace>
    <m:internalexception>
      <m:message>'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter.</m:message>
      <m:type>System.Runtime.Serialization.SerializationException</m:type>
+4
source share
3 answers

The web.api controller method was not requested by default, so the client did not complete the transaction. Added annotation for correction:[Queryable(AllowedOrderByProperties = "Id")]

+4
source

Try adding the code below to the WebApiConfig.cs file.

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);

I think the first two lines are optional if you are not using Json format.

. http://social.msdn.microsoft.com/Forums/vstudio/en-US/a5adf07b-e622-4a12-872d-40c753417645/web-api-error-the-objectcontent1-type-failed-to-serialize-the-response-body-for-content?forum=wcf

+1

I think you should make sure all relationships are loaded. As a workaround, you can create a new DTO (data transfer object) and put everything you need into it.

0
source

Source: https://habr.com/ru/post/1534273/


All Articles