Is there a way to get all the querystring name/value pairs into a collection?

前端 未结 5 2024
傲寒
傲寒 2020-12-01 10:02

Is there a way to get all the querystring name/value pairs into a collection?

I\'m looking for a built in way in .net, if not I can just split on the & and load

相关标签:
5条回答
  • 2020-12-01 10:36

    If you have a querystring ONLY represented as a string, use HttpUtility.ParseQueryString to parse it into a NameValueCollection.

    However, if this is part of a HttpRequest, then use the already parsed QueryString-property of your HttpRequest.

    0 讨论(0)
  • 2020-12-01 10:38

    QueryString property in HttpRequest class is actually NameValueCollection class. All you need to do is

    NameValueCollection col = Request.QueryString;

    0 讨论(0)
  • 2020-12-01 10:41

    Well, Request.QueryString already IS a collection. Specifically, it's a NameValueCollection. If your code is running in ASP.NET, that's all you need.

    So to answer your question: Yes, there is.

    0 讨论(0)
  • 2020-12-01 10:52

    You can use LINQ to create a List of anonymous objects that you can access within an array:

    var qsArray = Request.QueryString.AllKeys
        .Select(key => new { Name=key.ToString(), Value=Request.QueryString[key.ToString()]})
        .ToArray();
    
    0 讨论(0)
  • 2020-12-01 10:57

    Yes, use the HttpRequest.QueryString collection:

    Gets the collection of HTTP query string variables.

    You can use it like this:

    foreach (String key in Request.QueryString.AllKeys)
    {
        Response.Write("Key: " + key + " Value: " + Request.QueryString[key]);
    }
    
    0 讨论(0)
提交回复
热议问题