简体   繁体   English

.net 内核中的 HttpRequest.Query[] 是如何工作的?

[英]How does HttpRequest.Query[] in .net core works?


How does the below statement work?以下语句如何工作?

I tried to see in Microsoft documentation, but couldn't find much information我试图在 Microsoft 文档中查看,但找不到太多信息

Link to Microsoft documentation 链接到 Microsoft 文档

var queryString = this.Request.Query[SomeConstant.QueryString];

Lets assume you hit any of your endpoint with让我们假设您使用任何端点

/someendpoint?foo=bar&name=Vamsi

You can now get the whole QueryCollection with:您现在可以通过以下方式获取整个 QueryCollection:

var queries = this.Request.Query;

If you want to retrieve a specific value by its key, you can use that:如果你想通过它的键检索一个特定的值,你可以使用它:

var foo = this.Request.Query["foo"]   //value: bar
var name = this.Request.Query["name"] //value: Vamsi

So to answer your question: Query[SomeConstant.QueryString] is accessing the QueryCollection of the current request by accessing a specific key, that is stored in a variable called SomeContant.QueryString因此,回答您的问题: Query[SomeConstant.QueryString]正在通过访问特定键来访问当前请求的 QueryCollection,该键存储在名为SomeContant.QueryString的变量中

The Query object here is an IQueryCollection这里的查询 object 是一个 IQueryCollection

The IQueryCollection Implements both: IQueryCollection 实现了两者:

IEnumerable<KeyValuePair<String,StringValues>>
IEnumerable

suppose we have the following Url: http://localhost/home/index?code=A000假设我们有以下 Url: http://localhost/home/index?code=A000

The key value pair you can see it as a dictionnary, we have a key that represent the query string parameter name (ex: code) and we had a value (ex: A000)您可以将其视为字典的键值对,我们有一个表示查询字符串参数名称的键(例如:代码),我们有一个值(例如:A000)

In order to retreive the code from the url, you have to search in that list and find this name.为了从 url 中检索代码,您必须在该列表中搜索并找到该名称。 To do that, you call Query["code"]为此,您调用Query["code"]

In your case SomeConstant.QueryString is a constant defined somewhere in your projet in a class with name SomeConstant and a const with name QueryString and the value of this const is "code" .在您的情况下SomeConstant.QueryString是在您的项目中某处定义的常量,在 class 中,名称为SomeConstant和名称为QueryString的常量,此常量的值为"code"

public Class SomeConsant{
    public const string SomeConstant = "code";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM