C # obtain URL parameter values

Source: Internet
Author: User

When writing a program, we often need to pass parameters on the page, such as page? Id = 1234, you can directly use string id = Request. QueryString ["id"]; on the page to obtain the value of the parameter id 1234. This is a basic knowledge that everyone knows.

The above method: Request. queryString, which analyzes the input URL and stores the result in a Collection of key values. We only need to set the key value, you can return the value corresponding to this key.

Assuming that this URL is not coming in through the Request, we can't get the URL parameter value through the Request, can we use a string url = "http://www.google.com/page? Id = 1234 "what about the Collection [key] = value? In addition, our URL parameters are URL encoded. This Encoding is usually Encoding. UTF8 or Encoding. getEncoding ("gb2312"), please note that the URL of Baidu uses gb2312, while Google uses UTF8. How can we determine whether to use gb2312 or UTF8 to decode the input string?

In the Baidu or Google search for related solutions, you will see this statement, that is, matching the URL through a regular expression, such as: Regex urlRegex = new Regex (@"(? : ^ |? | &) Courseid = (d *)(? : & | $) "); But I doubt the data integrity analyzed through this method. Is it possible to ensure that all data is matched? Later, I thought of simulating the Request Method in Microsoft. NET Framework to solve this problem. In fact, the above string id = Request. queryString ["id"]; can also be written as System. collections. specialized. nameValueCollection col = Request. queryString; string strID = col ["id"]; so let's construct this NameValueCollection!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.