How to get GET parameters with ASP.NET MVC ApiController

后端 未结 8 1353
挽巷
挽巷 2021-01-07 16:54

I feel a bit absurd asking this but I can\'t find a way to get parameters for a get request at /api/foo?sort=name for instance.

In the ApiControll

相关标签:
8条回答
  • 2021-01-07 17:38

    The ApiController is designed to work without the HttpContext object (making it portable, and allowing it to be hosted outside of IIS).

    You can still access the query string parameters, but it is done through the following property:

    Request.GetQueryNameValuePairs()
    

    Here's an example loop through all the values:

    foreach (var parameter in Request.GetQueryNameValuePairs())
    {
         var key = parameter.Key;
         var value = parameter.Value;
    }
    
    0 讨论(0)
  • 2021-01-07 17:45

    You're trying to build an OData webservice? If so, just return an IQueryable, and the Web API will do the rest.

    0 讨论(0)
提交回复
热议问题