vendredi 4 août 2017

Return valid JsonApi result from Asp.Net Core (.Net Framework) API

I'm using an emberjs app against my web api.
In my "real" project, in ember I use the asp-web-api adapter.

But experimenting and created an asp.net core web api (.net framework) and trying to leave a new emberjs app using the JSON API adapter.

So this means I need to return a valid JSON API result from my api.

I achieved this as follows, not sure if there's a simpler way (for instance I really don't like having to set the type on my ViewModel)

        [HttpGet]
    public ContentResult Get()
    {
        IList<ProductTypeViewModel> producttypes = ProductTypeFactory.GetAll();
        var result = JsonConvert.SerializeObject(producttypes , new JsonApiSerializerSettings());
        return Content(result);
    }

    public class ProductTypeViewModel
{
   public string Type { get; set; } = "producttype"; //sets type so jsonapi result is right : TODO: I don't like this...
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}




Aucun commentaire:

Enregistrer un commentaire