1
    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("acDetails")]
    [ResponseType(typeof(List<AutoCompleteCombine>))]
    public IHttpActionResult acDetails(string Text)
    {
                    var db = new ModelName();
                    var DetailsList = (IList)null;
                    try
                    {
                        var DetailsIListXX = (from a in db.Tablename
                                          where a.ColumnName.StartsWith(Text)
                                          select a.ColumnName).Distinct().ToList();
                        DetailsList = DetailsIListXX.Select(x => new { ColumnNameChange= x }).ToArray();
                    }
                    catch (Exception ex)
                    {

                    }
                    return Ok(DetailsList);
     }

//properties
    public class AutoCompleteCombine
    {
        public string ColumnNameChange { get; set; }

    }

Above code works for 'application/json' and 'text/json' response types and gets error for application/xml and text/xml responses in swagger.

Exception Message:The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'

converting the response To List with AutoCompleteCombine class properties would fix the error i think.

Any suggestions to convert ilist to List in this object type case

Thanks

mr_gemini
  • 53
  • 4
  • Does the response give a 200 OK? If you are getting 200 than the data in response is not xml or there is a header that you have to remove. Xml should start with . If you are not getting 200 OK than something is wrong with the request. Post the error. – jdweng Apr 14 '20 at 17:31
  • 200 ok for application/json and text/json response types....and error for application/xml and text/xml responses....and the Exception Message for XML response types: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'-@jdweng – mr_gemini Apr 14 '20 at 17:58
  • Ignore the Xml error until you get a valid 200 OK in the response. First we have to understand why the XML request is failing. Do you have an examples of a working XML Request? What error are you getting instead of the 200 OK. Is it 500 or something else. – jdweng Apr 14 '20 at 18:17

1 Answers1

1

What you are doing with this line:

DetailsList = DetailsIListXX.Select(x => new { ColumnNameChange= x }).ToArray()

is creating Array of anonymous types (see new { ColumnNameChange...}), but instead you need to return List<AutoCompleteCombine>:

DetailsIListXX.Select(x => new AutoCompleteCombine { ColumnNameChange = x }).ToList();
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42