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:

using LinqToQuerystring.WebApi;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

 

namespace LinqStringAPI.Controllers

{

    public class ValuesController : ApiController

    {

        // GET api/values

      [LinqToQueryable]

        public IQueryable<string> Get()

        {

            return new string[] { "Smith", "Jhon", "Kabir", "Jony", "Hemant", "Robbin" }.AsQueryable();

        }

 

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:

var xlfm = config.Formatters.XmlFormatter;

            config.Formatters.Remove(xlfm);

The entire code looks like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web.Http;

 

namespace LinqStringAPI

{

    public static class WebApiConfig

    {

        public static void Register(HttpConfiguration config)

        {

            var xlfm = config.Formatters.XmlFormatter;

            config.Formatters.Remove(xlfm);

            config.Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "api/{controller}/{id}",

                defaults: new { id = RouteParameter.Optional }

            );

            config.EnableSystemDiagnosticsTracing();

        }

    }

}

 

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

Get Json Value

Up Next
    Ebook Download
    View all
    Learn
    View all