A large amount of querystring was used in the ASP.net startkit timetracker
For example:
http://localhost/TTWebCSVS_cn/ProjectList.aspx?index=2
Now let us in IE address typing the following address, what will happen?
Http://localhost/TTWebCSVS_cn/ProjectList.aspx?index=a2
Or
http://localhost/TTWebCSVS_cn/ProjectList.aspx?index=
It is obvious that the parameter index requires an integer to be received. And the address we typed was not in accordance with the requirements, exception.
Is there any way to prevent such a situation from happening?
We can actually define a page base class.
public class PageBase:System.Web.UI.Page
Allow other ASPX pages in the system to inherit pagebase.
The method of obtaining the numerical value in the QueryString is placed in the base class.
Write 3 methods in the base class.
<summary>/**////Get querystring parameter value///</summary>///<param name= "Querystringname" > The name of the QueryString parameter </param>///<param name= The value of the "result" >querystring parameter (string type) </param> protected
void Getquerystringvalue (string querystringname,out string result) {Result=string.empty;
Result=request.querystring[querystringname];
No parameters, exception, go to error page if (result==null | | result== "") {Errorpageredirect (); }}/**////<summary>///Gets the value of querystring parameter///</summary>///<param name= "Querystri Ngname ">querystring parameter's name </param>///<param name= value of" result ">querystring parameter (int type) </param> Pro
tected void Getquerystringvalue (string querystringname,out int result) {string str;
Getquerystringvalue (Querystringname,out str);
result=0;
try {result=convert.toint32 (str); } CATCH (OverflowException) {//less than minvalue of int or maxvalue greater than int, exception, go to error page errorpageredirect ();
catch (FormatException) {//Non-numeric character, exception, go to out error page Errorpageredirect ();
catch (ArgumentException) {//null reference, exception, go to error page errorpageredirect ();
The page turned///</summary> protected void Errorpageredirect ()/**////<summary>///error occurred.
{Response.Redirect ("errorquerystring.aspx", true); }