Using The LinqToQuerystring in Web API

Introduction

In this article you will see the simple use of LinqToQuerystring in the Web API. It works as an expression parser in .NET. It has the main purpose of supplying a lightweight subset of the OData URI. The aspects of the LinqQueryString is the greater flexibility of the OData. We use the "[LinqQueryable]" attribute in the ApiController.

Use the following procedure to create an example.

Step 1

Create a web API application as in the following:

  • Start Visual Studio 2012.
  • From the Start window select "New Project".
  • Then select "Installed" -> "Visual C#" -> "Web".
  • Select "MVC4 Web Application" and click on the "OK" button.

    Select MVC4 Application

  • From the "MVC4" window select "Web API".

    Select Web API

Step 2

Now install in the LinqToQuerystring package as in the following:

  • Go to the "Tools" menu then select "Library Package Manager" -> "Package Manager Console".

    Select Console Package Manager

  • In the Console window Type Command "Install-package LinqToQuerystring".
  • And press Enter.

After installing this package you will see a window like this one:

install LinqToQuerystring Package

Step 3

Now install another package "LinqToQuerystring.WebApi" using the same procedure as Step 2. Just use the command "install-package LinqToQuerystring.WebApi".

Step 4

Now we add some code in our ApiController. We Change the "IEnumrable" to"IQuerable" and add some value for reteriving the value.

  • In the "Solution Explorer".
  • Expand the "Controller" folder .
  • Select the "ValuesController".

    Select API Controller

Add the following code:

  1. using LinqToQuerystring.WebApi;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. namespace LinqStringAPI.Controllers  
  9. {  
  10.     public class ValuesController : ApiController  
  11.     {  
  12.         // GET api/values  
  13.         [LinqToQueryable]  
  14.         public IQueryable<string> Get()  
  15.         {  
  16.             return new string[] { "Smith""Jhon""Kabir""Jony""Hemant""Robbin" }.AsQueryable();  
  17.         }  
  18. } 

Step 5

Execute the application and browse it on this URL "http://localhost:20667/api/values":

Get XMl Value

Step 6

Now in the "WebApiConfig.cs" add these two lines of code:

 

  1. var xlfm = config.Formatters.XmlFormatter;  
  2. config.Formatters.Remove(xlfm);  

 

The entire code looks like this:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web.Http;  
  5. namespace LinqStringAPI  
  6. {  
  7.     public static class WebApiConfig  
  8.     {  
  9.         public static void Register(HttpConfiguration config)  
  10.         {  
  11.             var xlfm = config.Formatters.XmlFormatter;  
  12.             config.Formatters.Remove(xlfm);  
  13.             config.Routes.MapHttpRoute(  
  14.                 name: "DefaultApi",  
  15.                 routeTemplate: "api/{controller}/{id}",  
  16.                 defaults: new { id = RouteParameter.Optional }  
  17.             );  
  18.             config.EnableSystemDiagnosticsTracing();  
  19.         }  
  20.     }  
  21. } 

Now again execute the application; it then displays the JSON result:

Get Json Value