转换HTML危险字符的代码比如说我在文本框里有写
<script></script>
我提交以后转换成
&lt;script&gt;&lt;/script&gt;
也就是HTML格式的。过滤字符的方法

解决方案 »

  1.   

    string _Temp = "<script></script> ";            string _OkText =_Temp.Replace("<","&lt;").Replace(">","&gt;");
      

  2.   


     /// <summary>
            /// 过滤危险脚本
            /// </summary>
            /// <param name="htmlCode"></param>
            /// <returns></returns>
            public string wipeScript(string htmlCode)
            {
                System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\s]", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\s]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\s]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\s]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\s]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                htmlCode = regex1.Replace(htmlCode, ""); //过滤<script></script>标记 
                htmlCode = regex2.Replace(htmlCode, ""); //过滤href=javascript: (<a>) 属性 
                htmlCode = regex3.Replace(htmlCode, " _disibledevent="); //过滤其它控件的on...事件 
                htmlCode = regex4.Replace(htmlCode, ""); //过滤iframe 
                htmlCode = regex5.Replace(htmlCode, ""); //过滤frameset 
                return htmlCode;
            }
      

  3.   

    汗.. 没看清
    如果只是 转 &gt; 什么的 这个就行
    Server.HtmlEncode()