Reputation: 10152
In my project CloudMiddleware a have services for integrations with API like, PayPal, Twilio, etc. I have endpoints for SOAP, REST and AJAX requests, I want to use ODATA flexibility also because the request is via HTTP using the url itself. Is this possible?
Upvotes: 0
Views: 2303
Reputation: 10152
Suppose you have a controller for the services. Each service is a instance of the class that has the Id and Description. The service class has the Id and Description properties.
public class ServiceController : ApiController
{
// GET api/service
public IEnumerable<Service> Get()
{
return new Service[]
{
new Service { Id = 1, Description = "This is my service 1." },
new Service {Id = 2, Description = "This is my service 2."},
new Service {Id = 3, Description = "This is my service 3."}
};
}
// GET api/service/5
public Service Get(int id)
{
return null;
}
// POST api/service
public void Post([FromBody]string value)
{
}
}
public class Service
{
public int Id { get; set; }
public string Description { get; set; }
}
In order, to use the services of the controller via OData, you must use the attribute [Queryable] in the method "Get" and change the return type to IQueryable , and all ready!!!!!, being as follows:
public class ServiceController : ApiController
{
// GET api/service
[Queryable(ResultLimit = 10)]
public IQueryable<Service> Get()
{
return new Service[]
{
new Service { Id = 1, Description = "This is my service 1." },
new Service {Id = 2, Description = "This is my service 2."},
new Service {Id = 3, Description = "This is my service 3."}
}.AsQueryable();
}
// GET api/service/5
public Service Get(int id)
{
return null;
}
// POST api/service
public void Post([FromBody]string value)
{
}
}
The attribute Queryable has the property ResultLimit, it is used to express the greatest amount of service instances that can hold the result. It also has the properties LambdaNestingLimit, HandleNullPropagation and EnsureStableOrdering.
A request to /api/service?$top=2 returns a Json response:
{ { "Id": "1", "Description": "This is my service 1."}, { "Id": "2", "Description": "This is my service 2."}}
Upvotes: 1
Reputation: 3959
Yes, you can create OData endpoints in an ASP.NET MVC project using ASP.NET Web API or WCF Data Services. The former gives you more control and flexibility in the implementation of the endpoints.
Upvotes: 0