HtmlEncode from Class Library

后端 未结 8 1153
情书的邮戳
情书的邮戳 2020-11-29 23:15

I have a class library (in C#). I need to encode my data using the HtmlEncode method. This is easy to do from a web application. My question is, how do I use this method fro

相关标签:
8条回答
  • 2020-11-29 23:55

    System.Net.WebUtility class is available starting from .NET 4.0 (You donʼt need System.Web.dll dependency).

    0 讨论(0)
  • 2020-11-29 23:55

    Add a reference to System.Web.dll and then you can use the System.Web.HtmlUtility class

    0 讨论(0)
  • 2020-11-29 23:56

    If you are using C#3 a good tip is to create an extension method to make this even simpler. Just create a static method (preferably in a static class) like so:

    public static class Extensions
    {
        public static string HtmlEncode(this string s)
        {
            return HttpUtility.HtmlEncode(s);
        }
    }
    

    You can then do neat stuff like this:

    string encoded = "<div>I need encoding</div>".HtmlEncode();
    
    0 讨论(0)
  • 2020-11-30 00:04

    Try this

    System.Net.WebUtility.HtmlDecode(string);
    System.Net.WebUtility.HtmlEncode(string);
    
    0 讨论(0)
  • 2020-11-30 00:04

    Just reference the System.Web assembly and then call: HttpServerUtility.HtmlEncode

    http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.htmlencode.aspx

    0 讨论(0)
  • 2020-11-30 00:06

    Import System.Web Or call the System.Web.HttpUtility which contains it

    You will need to add the reference to the DLL if it isn't there already

    string TestString = "This is a <Test String>.";
    string EncodedString = System.Web.HttpUtility.HtmlEncode(TestString);
    
    0 讨论(0)
提交回复
热议问题