ASP.NET Web API IQueryable<T> challenge

asp.net-web-api

Solution

@SLacks is correct that you should return `IQueryable<object>` or `IQueryable<someBaseType>` if you can.

The error your getting is a function of the DataContract Serializer. So you have a few options.

- Switch to an alternate xml serlializer that supports what you want.

- Swtitch to a form of output that bypasses the serializer at issue (say JSON using JSON.net)

- Teach the data contract serializer how to serialize your object using the

For the "teach" option, you can teach in two ways.

(A) leverage the `[KnownType(typeof(...))]` attribute. Here's a post on the `KnownType` attribute. It's for WCF but should get you started.

(B) use a data contract resolver. This post should get you started.

Problem

I want to use the following pattern in my controllers: api/{controller}/{id}/{collection} Example: api/customers/123/addresses But I want to return `IQueryable Of T` from the corresponding Get action. I want something like this (simplified): ``` public IQueryable<????> GetCollection(int id, string collection) { switch(collection) { case "addresses": return _addresses.AsQueryable(); break; case "orders": return _orders.AsQueryable(); break; default: throw new Exception(NotSupported); } } ``` Can this be done? What would be the recommended approach?

Original source

Related problems