HtmlEncode from Class Library

后端 未结 8 1232
情书的邮戳
情书的邮戳 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: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 = "
    I need encoding
    ".HtmlEncode();

提交回复
热议问题