I'm playing with the new OData stuff but am getting a 406 when I try and request my data. Even when its just a simple with request with no OData filters. Example of my code below. Any suggestions as to why I might be getting this?
public class ValuesController : ODataController
{
[Queryable(PageSize=5)]
public IQueryable<Test> Get()
{
return new List<Test>
{
new Test { Id = 1, Name = "Test1"},
new Test { Id = 2, Name = "Test2"},
new Test { Id = 3, Name = "Test3"},
new Test { Id = 4, Name = "Test4"}
}.AsQueryable();
}
}
public class Test
{
public string Name { get; set; }
public int Id { get; set; }
}
You need to define an entity model in your configuration. The easiest way is using ODataConventionModelBuilder:
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Test>("Values");
Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
In this example 'Test' is your model and "Values" is the name of the entity set, which defines the OData URIs. In your controller, you still return Test instances -- the controller doesn't need to know about the EDM model. Also note, using
the default conventions, the controller name needs to match the name of the entity set ("Values"/ValuesController in this example).
invalid_arg
0 Points
4 Posts
406
Feb 20, 2013 01:09 PM|LINK
Hey Guys,
I'm playing with the new OData stuff but am getting a 406 when I try and request my data. Even when its just a simple with request with no OData filters. Example of my code below. Any suggestions as to why I might be getting this?
public class ValuesController : ODataController { [Queryable(PageSize=5)] public IQueryable<Test> Get() { return new List<Test> { new Test { Id = 1, Name = "Test1"}, new Test { Id = 2, Name = "Test2"}, new Test { Id = 3, Name = "Test3"}, new Test { Id = 4, Name = "Test4"} }.AsQueryable(); } } public class Test { public string Name { get; set; } public int Id { get; set; } }MikeWasson
Member
499 Points
79 Posts
Microsoft
Re: 406
Feb 20, 2013 04:50 PM|LINK
Did you call MapODataRoute in your configuration?
invalid_arg
0 Points
4 Posts
Re: 406
Feb 20, 2013 07:53 PM|LINK
I did not. I will try that.
Thanks
Mat
MikeWasson
Member
499 Points
79 Posts
Microsoft
Re: 406
Feb 20, 2013 08:36 PM|LINK
See http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/getting-started-with-odata-in-web-api/create-a-read-only-odata-endpoint under "Configuration"
- Mike
invalid_arg
0 Points
4 Posts
Re: 406
Feb 21, 2013 08:08 AM|LINK
I've read that post. Serves me right for scanning.
Thanks
invalid_arg
0 Points
4 Posts
Re: 406
Feb 21, 2013 01:00 PM|LINK
Am I missing something obvious because I don't have an Entity Model?
I'm just wanting to perform this on a list of models
MikeWasson
Member
499 Points
79 Posts
Microsoft
Re: 406
Feb 21, 2013 05:34 PM|LINK
You need to define an entity model in your configuration. The easiest way is using ODataConventionModelBuilder:
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet<Test>("Values"); Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();In this example 'Test' is your model and "Values" is the name of the entity set, which defines the OData URIs. In your controller, you still return Test instances -- the controller doesn't need to know about the EDM model. Also note, using the default conventions, the controller name needs to match the name of the entity set ("Values"/ValuesController in this example).