Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Json Using PetaPoco Dynamic & WebAPI

Is it possible to use PetaPoco dynamic query to return Json in the ASP.net WebAPI?

//WebAPI Controller

public class BranchController : ApiController
{
    public IEnumerable<dynamic> Get()
    {
        // Create a PetaPoco database object
        var db = new PetaPoco.Database("DefaultConnection");

        // Show all Branches
        var b = db.Query<dynamic>("SELECT * FROM Branches").ToList();

        return b;
    }

}

I am receiving an error

To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object)

like image 499
MHop Avatar asked Nov 04 '22 01:11

MHop


1 Answers

JSON.Net handles this out of the box, so I Had to add a Custom Formatter.

This is the MSDN article I used to resolve the issue: http://code.msdn.microsoft.com/Using-JSONNET-with-ASPNET-b2423706

like image 139
MHop Avatar answered Nov 09 '22 15:11

MHop