为什么将StringValues用于Request.Query值? [英] Why is StringValues used for Request.Query values?

查看:615
本文介绍了为什么将StringValues用于Request.Query值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一些看起来像这样的网址:www.myhost.com/mypage?color=blue

Let's say I have some url that looks like this: www.myhost.com/mypage?color=blue

在Asp.Net Core中,我希望通过执行以下操作来获取颜色查询参数值:

In Asp.Net Core, I'd expect to get the color query parameter value by doing the following:

string color = Request.Query["color"];

但是事实证明,Request.Query["color"]返回的是类型StringValues而不是string的值.为什么会这样?

But it turns out that the Request.Query["color"] returns a value of type StringValues rather than string. Why is that?

显然,StringValues类型可以容纳字符串数组,并包括对隐式转换为string[]的支持,这很酷,但是为什么查询参数值需要它呢?

Apparently the StringValues type can hold an array of strings and includes support for implicit conversion to string[] which is cool but why is that needed for a query param value?

获得这样的值似乎很奇怪:

Having to get the value like this seems odd:

string color = Request.Query["color"].ToString();

更糟糕的是,不再可以像这样检查值以查看是否指定了查询参数

And worse, checking for a value to see if a query param is specified can no longer be done like so

  if(Request.Query["color"] == null) { 
      //param was not specified
  }

但必须像这样检查

 if(Request.Query["color"].Count == 0) { 
      //param was not specified
 }

由于一个查询参数不能有多个值(据我所知),为什么Request.Query["color"]返回一个StringValues对象而不是字符串?

Since a single query parameter can't have multiple values (as far as I know) why does Request.Query["color"] return a StringValues object rather than a string?

推荐答案

正如其他人已经提到的,该类型是StringValues对象,因为从技术上讲,允许多个值.尽管通常的做法是仅设置一个值,但URI规范并不禁止多次设置值.由应用程序决定如何处理.

As already mentioned by others, the type is a StringValues object because technically, multiple values are allowed. While the common practice is to just set a single value, the URI specification does not disallow setting values multiple times. And it’s up to the application to decide how to handle that.

话虽这么说,StringValues隐式转换为string,所以您实际上不需要在其上调用ToString(),您可以像使用字符串一样使用它.因此,可以执行Request.Query["color"] == "red"之类的操作,或将其传递给期望字符串的方法.

That being said, StringValues has an implicit conversion to string, so you don’t actually need to call ToString() on it, you can just use it as if it was a string. So doing things like Request.Query["color"] == "red", or passing it to a method that expects a string will just work.

更糟糕的是,不再像这样Request.Query["color"] == null那样完成检查值以查看是否指定了查询参数的操作,而必须象Request.Query["color"].Count == 0

And worse, checking for a value to see if a query param is specified can no longer be done like so Request.Query["color"] == null but instead must be checked like so Request.Query["color"].Count == 0

只有一半是正确的.是的,为了检查StringValues对象是否为空,可以检查其Count属性.您还可以对照StringValues.Empty:

That’s only half true. Yes, in order to check whether a StringValues object is empty, you can check its Count property. You can also check against StringValues.Empty:

Request.Query["color"] == StringValues.Empty

但是,最初的问题"是Request.Query[x]总是返回一个非空的StringValues对象(因此可以安全地检查任何值).如果要检查查询参数中是否存在键,则应使用ContainsKey:

However, the initial "issue" is that Request.Query[x] will always return a non-null StringValues object (so it’s safe to check for any value). If you want to check whether a key exists in the query arguments, you should use ContainsKey:

if (Request.Query.ContainsKey("color"))
{
    // only now actually retrieve the value
    string colorValue = Request.Query["color"];
}

或者使用TryGetValue:

if (Request.Query.TryGetValue("color", out var colorValue))
{
    DoSomething(colorValue);
}


总而言之,大多数时候实际上并不需要访问Request.Query.您应该只使用模型绑定相反,只要将它们放在操作的签名中,它将自动为您提供所需的查询参数:


That all being said, accessing Request.Query is not really necessary most of the times. You should just use make use of model binding instead which will automatically give you the query arguments you need by just having them in the action’s signature:

public ActionResult MyAction(string color)
{
    DoSomething(color);
}

这篇关于为什么将StringValues用于Request.Query值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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