ASP. NET

Source: Internet
Author: User

ASP. net, if you want to assign a URL parameter value to a variable, you must first determine whether the parameter exists. Otherwise, it is likely that you have not set the object reference to the instance of the object ", used to be
Request. querystring ["XX"]! = NULL is compared before the value is assigned, but an error is still reported in a project today. The code structure is roughly as follows:

Copy content to clipboard

Program code int id = 0;
If (request. querystring ["ID"]! = NULL)
Id = int. parse (request. querystring ["ID"]);

After
To find the URL is: http://www.mzwu.com /? Id =, because the value of request. querystring ["ID"] is an empty string
Request. querystring ["ID"]! =
Null verification, but pass the null string to the int. the parse method still reports an error. We can see that it is not enough to determine whether the parameter exists. We must further determine whether the parameter has been passed.
Code changed:

Copy content to clipboard

Program code int id = 0;
If (! String. isnullorempty (request. querystring ["ID"])
Id = int. parse (request. querystring ["ID"]);

The reason is: when the parameter does not exist, the parameter value is null and the string is called. isnullorempty method. null is automatically converted to an empty string. If a parameter exists but does not pass a value, the parameter value is a Null String, String. isnullorempty is easy to tell!

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.