I have written an endpoint in ASP.net (which should give back a nice JSON) and the following error puzzles me since via my Swagger interface, everything works fine, but if I call the endpoint directly in a browser with http://localhost:63291/api/AutoUpload/
, I receive:
Internal exception has occured: Type '<>f__AnonymousType2`6[System.String,System.DateTime]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
This seems strange, because I was thinking that I did indeed implement a parameter-less (default) constructor. My code reads as follows
using AutoMapper;
using myProject.API.Filters;
using myProject.API.Models;
using myProject.Entity.DAL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
namespace myProject.API.Controllers
{
[UserAuthenticationFilter]
public class AutoUploadController : BaseController
{
public AutoUploadController() {
System.Diagnostics.Debug.WriteLine("Parameterless default constructor only for serialization");
}
// GET: api/AutoUpload
[ResponseType(typeof(IEnumerable<FilesDetailsDto>))]
public IHttpActionResult GetAutoUpload(string OptionString = "status")
{
if (OptionString == "status")
{
var rootLocation = ConfigurationManager.AppSettings["ROOT-FOLDER-LOCATION"];
string[] entries = Directory.GetFiles(rootLocation, "*.csv", SearchOption.AllDirectories);
var convList = entries.Select(x => new
{
FullPath = x,
LastModifed = System.IO.File.GetLastWriteTime(x)
});
return Ok(convList.AsEnumerable());
} // end IF-clause for optional parameter
else
{
return NotFound();
}
}
}
}
The above piece of code uses the following data transfer object (DTO) definition:
using System;
namespace myProject.API.Models
{
public class FilesDetailsDto
{
public string FileName { get; set; }
public DateTime LastModifiedOnFilesystem { get; set; }
}
}