@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

March 22, 2012 comment , , , ,

OData service with ASP .NET Web API

This post is part of ASP .NET MVC 4 Article Series.

With the release of ASP.NET Web API, Microsoft has unified programming model for building HTTP services which can target various range of client including mobile and tablet devices. Moreover WCF Web API is also now ASP.NET Web API.

In previous post we have seen what is ASP.NET Web API and why we should use it. In this post we will see how to build OData services with ASP.NET Web API.

What is OData service?

The OData (Open Data Protocol) is a Web protocol for querying and updating data. OData enable you to develop a service that can be accessed and queried by URI conventions. Consumer can query, filter, sort data by forming standard URI. More information on URI conventions can be found here.

ASP.NET Web API and OData

Building OData services with ASP.NET Web API is quite easier than WCF Data service. We can create OData service by simply returning IQueryable<T> from API Controller action method. Once action method return IQueryable<T>, we can consume it by following OData URI conventions. Following code snippet shows action method which return IQueryable<T>.

public IQueryable&lt;Blog&gt; GetBlogList()
{
    return new List&lt;Blog&gt;
    {
        new Blog {Id=1, Author="Nandip Makwana", TotalPost=50},
        new Blog {Id=2, Author="Jalpesh Vadgama", TotalPost=300},
        new Blog {Id=3, Author="Dhananjay Kumar", TotalPost=500}
    }.AsQueryable();
}

We can clearly see that it is quite easier to build OData services with ASP.NET Web API. Now let consume this OData service by entering api/blog in browser. As you can see it returns all the three records. Now enter api/blog?$orderby=author in browser, result are now sorted on Author field see following image.

Now enter api/blog?$filter=author eq ’Nandip Makwana’ as you can see in following image, service has returned only one record which satisfy filter criteria.

More information on URI conventions can be found here.

Hope this post would be helpful.

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading