访问 ASP.Net Web Api 中的查询字符串? [英] Accessing the query string in ASP.Net Web Api?
问题描述
我使用的是由 Asp.net Web Api 生成的默认模板.我正在使用 Get() 部分:
I'm using the default template generated by Asp.net Web Api. I'm working with the Get() Part:
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
出于某种原因,我认为访问查询字符串唯一要做的就是创建一个输入字符串变量.因此,我为生成的默认控制器创建了另一个函数(我所做的唯一更改):
For some reason the i thought the only thing you had to do to access to query string was just to create an input string variable. So i created one more function (the only change i made) to the default controller generated:
public IEnumerable<string> Get(string queryString)
{
return new string[] { "value3", "value4" };
}
我在这两种方法中都设置了一个断点,但即使我添加了一个查询字符串,它也总是转到没有参数的函数.所以如果我去 http://mybaseurl/api/values?foo=f
它仍然会使用 Get() 而不是 get(string queryString).它不像我想的那样工作吗?我知道我可以使用 Request.RequestUri.ParseQueryString();
访问 Get() 函数中的查询字符串,但如果可能,我更喜欢将它分开.
I put a break point in both methods but even if i add a query string it always goes to the function with no parameters. So if i go to http://mybaseurl/api/values?foo=f
it still is going to Get() instead of get(string queryString). Does it not work the way i thought? I know i can access the query string in the Get() function using Request.RequestUri.ParseQueryString();
but i prefer to have it separated like this if possible.
推荐答案
查询字符串键名应与动作的参数名匹配:
The query string key name should match the parameter name of the action:
/api/values?queryString=f
/api/values?queryString=f
public IEnumerable<string> Get(string queryString)
{
return new string[] { "value3", "value4" };
}
这篇关于访问 ASP.Net Web Api 中的查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!