ASP. NET anti-injection two common functions; the string part is not very good

Source: Internet
Author: User

1. If all the parameters are numbers:
// Check whether all strings are numbers

Public static bool isnum (string Str)

{
Bool blresult = true;
If (STR = "")
Blresult = false;
Else
{
Foreach (char in Str)
{
If (! Char. isnumber (char ))
{
Blresult = false;
Break;
}
}
If (blresult)
If (Int. parse (STR) = 0)
Blresult = false;
}
Return blresult;

}

 

Application:
String topicid = request. querystring ["topicid"];

If (! Isnum (topicid ))
Server. Transfer ("error. aspx? Errid = 404 ");

2. If the parameter is text.

// Html Conversion

Public static string htmlstr (string CHR)

{
If (CHR = NULL)
Return "";
CHR = CHR. Replace ("<", "<");
CHR = CHR. Replace (">", "> ");
CHR = CHR. Replace ("/N", "<br> ");
CHR = CHR. Replace ("/"",""");
CHR = CHR. Replace ("'","'");
CHR = CHR. Replace ("","");
CHR = CHR. Replace ("/R ","");
Return (CHR );

}
Application: String strclass = htmlstr (request. querystring ["classname"]);

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.