Search code examples
c#.net-corehttprequest

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

Link to Microsoft documentation

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


Solution

  • Lets assume you hit any of your endpoint with

    /someendpoint?foo=bar&name=Vamsi

    You can now get the whole QueryCollection with:

    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