Code reading summary of ASP.net startkit Timetracker (QueryString's improved notes)

Source: Internet
Author: User

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); }

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.