Filter HTML string summary in ASP. NET, asp.net string

Source: Internet
Author: User
Tags asp net

Filter HTML string summary in ASP. NET, asp.net string

Write down for backup first!

 

C # code
  1. /// <Summary> remove HTML tags
  2. ///
  3. /// </Summary>
  4. /// <Param name = "Htmlstring"> including the source code of HTML </param>
  5. /// <Returns> removed text </returns>
  6. Public static string GetNoHTMLString (string Htmlstring)
  7. {
  8. // Delete the script
  9. Htmlstring = Regex. Replace (Htmlstring, @ "<script [^>] *?>. *? </Script> "," ", RegexOptions. IgnoreCase );
  10. // Delete HTML
  11. Htmlstring = Regex. Replace (Htmlstring, @ "<(. [^>] *)>", "", RegexOptions. IgnoreCase );
  12. Htmlstring = Regex. Replace (Htmlstring, @ "([\ r \ n]) [\ s] +", "", RegexOptions. IgnoreCase );
  13. Htmlstring = Regex. Replace (Htmlstring, @ "-->", "", RegexOptions. IgnoreCase );
  14. Htmlstring = Regex. Replace (Htmlstring, @ "<! --. * "," ", RegexOptions. IgnoreCase );
  15. Htmlstring = Regex. Replace (Htmlstring, @ "& (quot | #34);", "\" ", RegexOptions. IgnoreCase );
  16. Htmlstring = Regex. Replace (Htmlstring, @ "& (amp | #38);", "&", RegexOptions. IgnoreCase );
  17. Htmlstring = Regex. Replace (Htmlstring, @ "& (lt | #60);", "<", RegexOptions. IgnoreCase );
  18. Htmlstring = Regex. Replace (Htmlstring, @ "& (gt | #62);", ">", RegexOptions. IgnoreCase );
  19. Htmlstring = Regex. Replace (Htmlstring, @ "& (nbsp | #160);", "", RegexOptions. IgnoreCase );
  20. Htmlstring = Regex. Replace (Htmlstring, @ "& (iexcl | #161);", "\ xa1", RegexOptions. IgnoreCase );
  21. Htmlstring = Regex. Replace (Htmlstring, @ "& (cent | #162);", "\ xa2", RegexOptions. IgnoreCase );
  22. Htmlstring = Regex. Replace (Htmlstring, @ "& (pound | #163);", "\ xa3", RegexOptions. IgnoreCase );
  23. Htmlstring = Regex. Replace (Htmlstring, @ "& (copy | #169);", "\ xa9", RegexOptions. IgnoreCase );
  24. Htmlstring = Regex. Replace (Htmlstring, @ "& # (\ d +);", "", RegexOptions. IgnoreCase );
  25. Htmlstring. Replace ("<","");
  26. Htmlstring. Replace ("> ","");
  27. Htmlstring. Replace ("\ r \ n ","");
  28. Htmlstring = HttpContext. Current. Server. HtmlEncode (Htmlstring). Trim ();
  29. Return Htmlstring;
  30. }
  31. /// <Summary> obtain the displayed string to display HTML tags, but filter dangerous HTML tags, such as iframe and script.
  32. ///
  33. /// </Summary>
  34. /// <Param name = "str"> unprocessed string </param>
  35. /// <Returns> </returns>
  36. Public static string GetSafeHTMLString (string str)
  37. {
  38. Str = Regex. Replace (str, @ "<applet [^>] *?>. *? </Applet> "," ", RegexOptions. IgnoreCase );
  39. Str = Regex. Replace (str, @ "<body [^>] *?>. *? </Body> "," ", RegexOptions. IgnoreCase );
  40. Str = Regex. Replace (str, @ "<embed [^>] *?>. *? </Embed> "," ", RegexOptions. IgnoreCase );
  41. Str = Regex. Replace (str, @ "<frame [^>] *?>. *? </Frame> "," ", RegexOptions. IgnoreCase );
  42. Str = Regex. Replace (str, @ "<script [^>] *?>. *? </Script> "," ", RegexOptions. IgnoreCase );
  43. Str = Regex. Replace (str, @ "<frameset [^>] *?>. *? </Frameset> "," ", RegexOptions. IgnoreCase );
  44. Str = Regex. Replace (str, @ "
  45. Str = Regex. Replace (str, @ "<iframe [^>] *?>. *? </Iframe> "," ", RegexOptions. IgnoreCase );
  46. Str = Regex. Replace (str, @ "<style [^>] *?>. *? </Style> "," ", RegexOptions. IgnoreCase );
  47. Str = Regex. Replace (str, @ "<layer [^>] *?>. *? </Layer> "," ", RegexOptions. IgnoreCase );
  48. Str = Regex. Replace (str, @ "<link [^>] *?>. *? </Link> "," ", RegexOptions. IgnoreCase );
  49. Str = Regex. Replace (str, @ "<ilayer [^>] *?>. *? </Ilayer> "," ", RegexOptions. IgnoreCase );
  50. Str = Regex. Replace (str, @ "<meta [^>] *?>. *? </Meta> "," ", RegexOptions. IgnoreCase );
  51. Str = Regex. Replace (str, @ "<object [^>] *?>. *? </Object> "," ", RegexOptions. IgnoreCase );
  52. Return str;
  53. }

 


How does aspnet filter out html code?

Public static string NoHtml (string text)
{
// Delete the script
Text = Regex. Replace (text, @ "<script [^>] *?>. *? </Script> "," ", RegexOptions. IgnoreCase );
// Delete HTML
Text = Regex. Replace (text, @ "<(. [^>] *)>", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "([\ r \ n]) [\ s] +", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "-->", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "<! --. * "," ", RegexOptions. IgnoreCase );

Text = Regex. Replace (text, @ "& (quot | #34);", "\" ", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (amp | #38);", "&", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (lt | #60);", "<", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (gt | #62);", ">", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (nbsp | #160);", "", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (iexcl | #161);", "\ xa1", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (cent | #162);", "\ xa2", RegexOptions. IgnoreCase );
Text = Regex. Replace (text, @ "& (poun ...... the remaining full text>

Asp net filters html code

By default, POST requests containing HTML tags are prohibited. Set ValidateRequest = "false ".

For example:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = YourNameSpace. YourClassName "ValidateRequest =" false "%>

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.