I'm trying to use the Api web and getting this error when trying to serialize an array.
The type 'HT.Data.Client' with data contract name 'Client: link 'is not expected. Consider using a DataContractResolver if you are using a DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
My class that stays in a project other than the web API:
namespace HT.Data
{
[DataContract]
public partial class Client
{
[Key]
[DataMember(Order = 1, IsRequired = true)]
[Required(ErrorMessage = "Campo obrigatorio")]
[MaxLength(128, ErrorMessage = "Tamanho maximo do campo: 128 caracteres. ")]
public string Id { get; set; } // Id (Primary key)
[DataMember(Order = 2, IsRequired = true)]
[Required(ErrorMessage = "Campo obrigatorio")]
public string Secret { get; set; } // Secret
[DataMember(Order = 3, IsRequired = true)]
[Required(ErrorMessage = "Campo obrigatorio")]
[MaxLength(100, ErrorMessage = "Tamanho maximo do campo: 100 caracteres. ")]
public string Name { get; set; } // Name
[DataMember(Order = 4, IsRequired = true)]
[Required(ErrorMessage = "Campo obrigatorio")]
public int ApplicationType { get; set; } // ApplicationType
[DataMember(Order = 5, IsRequired = true)]
[Required(ErrorMessage = "Campo obrigatorio")]
public bool Active { get; set; } // Active
[DataMember(Order = 6, IsRequired = true)]
[Required(ErrorMessage = "Campo obrigatorio")]
public int RefreshTokenLifeTime { get; set; } // RefreshTokenLifeTime
[DataMember(Order = 7, IsRequired = false)]
[MaxLength(100, ErrorMessage = "Tamanho maximo do campo: 100 caracteres. ")]
public string AllowedOrigin { get; set; } // AllowedOrigin
public Client()
{
InitializePartial();
}
partial void InitializePartial();
}
My controller:
namespace App.ResourceServer.Controllers
{
//[Authorize]
[RoutePrefix("api/sample")]
public class SampleController : ApiController
{
private UnitOfWork db = new UnitOfWork();
[Route("")]
public IEnumerable<object> Get()
{
Client[] Result = db.ClientRepository.GetAllClients().ToArray();
return Result;
}
}
}
And initializing my API:
namespace AngularJSAuthentication.ResourceServer.App_Start
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonFormatter.SerializerSettings.ContractResolver.ResolveContract(typeof(Client));
}
}
}
How do I configure the class to be known / expected?