asp.net-mvc 如何使用 ASP.NET MVC ApiController 获取 GET 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10656841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get GET parameters with ASP.NET MVC ApiController
提问by xster
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=namefor instance.
我觉得问这个有点荒谬,但我找不到一种方法来获取获取请求的参数
/api/foo?sort=name,例如。
In the ApiControllerclass, I gave a public string Get(). Putting Get(string sort)makes /api/fooa bad request. Request instance in the ApiControlleris of type System.Net.Http.HttpRequestMessage. It doesn't have a QueryStringor Parametersproperty or anything.
在ApiController课堂上,我给了一个public string Get(). 推杆Get(string sort)是/api/foo一个糟糕的要求。中的请求实例ApiController类型为System.Net.Http.HttpRequestMessage。它没有 aQueryString或Parameters属性或任何东西。
采纳答案by Josh Mouch
You could just use
你可以用
HttpContext.Current.Request.QueryString
回答by Darren
The ApiController is designed to work without the HttpContext object (making it portable, and allowing it to be hosted outside of IIS).
ApiController 旨在在没有 HttpContext 对象的情况下工作(使其可移植,并允许它在 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;
}
回答by Mark
Here's an example that gets the querystring q from the request and uses it to query accounts:
这是一个从请求中获取查询字符串 q 并使用它来查询帐户的示例:
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
{
throw new Exception("Please specify a search query");
}
This can be called then like this:
这可以像这样调用:
url/api/Accounts?q=p
url/api/账户?q=p
回答by Chris Halcrow
Get all querystring name/value pairs into a variable:
将所有查询字符串名称/值对放入一个变量中:
IEnumerable<KeyValuePair<string, string>> queryString = request.GetQueryNameValuePairs();
Then extract a specified querystring parameter
然后提取指定的查询字符串参数
string value = queryString.Where(nv => nv.Key == "parameterNameGoesHere").Select(nv => nv.Value).FirstOrDefault();
回答by Khateeb321
You can also use the following
您还可以使用以下
var value = request.GetQueryNameValuePairs().Where(m => m.Key == "paramName").SingleOrDefault().Value;
回答by So_oP
if we have a proper model for that request
如果我们有适合该请求的模型
for example
例如
public class JustModel
{
public int Id {get;set;}
public int Age {gets;set;}
}
and query like this
并像这样查询
/api/foo?id=1&Age=10
You could just use [FromUri]attribute
你可以只使用 [FromUri]属性
For example
例如
public IHttpActionResult GetAge([FromUri] JustModel model){}
回答by Nime Cloud
Adding a default value does the job:
添加默认值可以完成以下工作:
public string Get(string sort="")
回答by Leon Cullens
You're trying to build an OData webservice? If so, just return an IQueryable, and the Web API will do the rest.
您正在尝试构建 OData 网络服务?如果是这样,只需返回一个 IQueryable,Web API 将完成剩下的工作。

