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!