访问在ASP.Net网页API查询字符串? [英] Accessing the query string in ASP.Net Web Api?

查看:159
本文介绍了访问在ASP.Net网页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 / foo的值= F
 它仍然是会得到(),而不是获得(字符串的queryString)。它不工作我以为的方式?我知道我可以使用访问查询字符串中的get()函数 Request.RequestUri.ParseQueryString(); 但我preFER把它分开这样如果可能的话

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 /价值?的queryString = F

/api/values?queryString=f

public IEnumerable<string> Get(string queryString)
    {
        return new string[] { "value3", "value4" };
    }

这篇关于访问在ASP.Net网页API查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆