Dailycode.info

Short solution for short problems

.Net Web API and querystrings

 

Creating a web api is very easy and powerfull. If you want to use querystrings, its not done with the ols Request.Querystrings, but rather like this:

publicIEnumerable<GALV_ACCOUNT> GetAllAccounts()

{

    var q = Request.GetQueryNameValuePairs().Where(nv => nv.Key =="q").Select(nv => nv.Value).FirstOrDefault();

    if (q != null && q != string.Empty)

    {

        var result = accounts.Where(a=>a.Name.ToLower().StartsWith(q.ToLower()));

        return result;

    }

    else

    {

        thrownewException("Please specify a search query");

    }

}

Then you can call the method like this:

http://myserver/Accounts?q=p