How to get Query Params in Web API ASP.Net C# from Request

Since ApiController was made to work without IIS Hosting, it does not depend upon the HttpContext object.

We need System.Net.Http namespace to get the extension method GetQueryNameValuePairs()
Request.GetQueryNameValuePairs()

To get the param values from the query string, we need another namespace System.Linq

URL: http://localhost/Person?query=test
Request.GetQueryNameValuePairs().Where(nv => nv.Key == "query").Select(nv => nv.Value).FirstOrDefault()

Leave a Reply