Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Monday, March 1, 2010

A potentially dangerous Request.Form value was detected from the client ASP.NET

I got this error message when I try to submit a form with HTML Code, the default settings of the .NET Framework will not allow me to submit HTML Code to prevent cross-site scripting (XSS) attacks.

To avoid this error set Validate Request to false in the Page Directive.
ValidateRequest="false"

Monday, February 1, 2010

How to remove HTML Tags from string in C#

I will show you three different methods to remove HTML tags from string in C#:

1. By using Regex:
public static string RemoveHTMLTags(string html)
{
 return Regex.Replace(html, "<.*?>", string.Empty);
}

2. By using Compiled Regex for better performance:
static Regex htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
   
public static string RemoveHTMLTagsCompiled(string html)
{
 return htmlRegex.Replace(html, string.Empty);
}

3. By using Char Array for faster performance for several HTML files:
public static string RemoveHTMLTagsCharArray(string html)
{
 char[] charArray = new char[html.Length];
 int index = 0;
 bool isInside = false;
  
 for (int i = 0; i < html.Length; i++)
  {
   char left = html[i];

   if (left == '<')
    {
     isInside = true;
      continue;
    }

   if (left == '>')
    {
     isInside = false;
     continue;
    }

   if (!isInside)
    {
     charArray[index] = left;
     index++;
    }
   }

 return new string(charArray, 0, index);
}

Tuesday, August 19, 2008

How to Ignore JavaScript errors

Sometimes when you are working on a page and want to publish it and you have some JavaScript errors, but you don't have time to debug it, you can put the following code in order to ignore the JavaScript errors:
<head>
<script language="javascript">
    function stoperror() {
        return true;
    }
    window.onerror = stoperror();
</script>
</head>