Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most optimal way to parse querystring within a string in C#

Tags:

string

c#

parsing

I have a querystring alike value set in a plain string. I started to split string to get value out but I started to wonder that I can proabably write this in one line instead. Could you please advice if there is more optimal way to do this?

I am trying to read "123" and "abc" like in Request.QueryString but from normal string.

 protected void Page_Load(object sender, EventArgs e) {     string qs = "id=123&xx=abc";     string[] urlInfo = qs.Split('&');     string id = urlInfo[urlInfo.Length - 2];     Response.Write(id.ToString());  } 
like image 201
jpkeisala Avatar asked Jul 30 '09 13:07

jpkeisala


People also ask

How do you parse a QueryString?

The ParseQueryString method uses UTF8 format to parse the query string In the returned NameValueCollection, URL-encoded characters are decoded and multiple occurrences of the same query string parameter are listed as a single entry with a comma separating each value.

What is request QueryString ()?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter). Count.

Which module is used to parse the QueryString in Nodejs?

The node:querystring module provides utilities for parsing and formatting URL query strings. It can be accessed using: const querystring = require('node:querystring');


1 Answers

You can do it this way:

using System.Collections.Specialized;  NameValueCollection query = HttpUtility.ParseQueryString(queryString); Response.Write(query["id"]); 

Hope it helps.

like image 72
Nelson Reis Avatar answered Oct 05 '22 14:10

Nelson Reis