请问如何过滤掉危险的HTML代码啊
我用了
str=str.Replace("<","&lt;");
str=str.Replace(">","&gt;");
存入数据库的也是对的,可是只要我一加字母就报错
就是说输入"<"存入数据库的是"&lt;",可是只要写成"<br"就报错
怎么弄啊?

解决方案 »

  1.   

    HttpUtility.HtmlDecode(str);
    HttpUtility.HtmlEncode(str);
    用者俩
      

  2.   

    我用了没有效果呀,
    我听说要在HTML页面里面加一句什么代码还是什么的?
    请问能指教下不
      

  3.   

    MSDN上的例子
    那两个方法是可以实现的你需求的
    using System;
    using System.Web;
    using System.IO;   class MyNewClass
       {
          public static void Main()
          {
             String myString;
             Console.WriteLine("Enter a string having '&' or '\"'  in it: ");
             myString=Console.ReadLine();
             String myEncodedString;
             // Encode the string.
             myEncodedString = HttpUtility.HtmlEncode(myString);
             Console.WriteLine("HTML Encoded string is "+myEncodedString);
             StringWriter myWriter = new StringWriter();
             // Decode the encoded string.
             HttpUtility.HtmlDecode(myEncodedString, myWriter);
             Console.Write("Decoded string of the above encoded string is "+
                            myWriter.ToString());
          }
       }
      

  4.   

    谢谢,我已经解决了...
    是在PAGE里面加一句话
      

  5.   

    楼主如此保守,还来csdn,帮与被帮是相互的,你提了问题,没留下正确答案就结了贴,不是给别人留下垃圾吗?