来自类库的 HtmlEncode

发布于 2024-07-26 20:30:06 字数 99 浏览 7 评论 0原文

我有一个类库(C# 语言)。 我需要使用 HtmlEncode 方法对数据进行编码。 这可以通过网络应用程序轻松完成。 我的问题是,如何使用从控制台应用程序调用的类库中的此方法?

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 from a class library that is being called from a console application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

风轻花落早 2024-08-02 20:30:06

System.Net.WebUtility class is
available starting from .NET Framework 4.0 — you donʼt need System.Web dependency.

友欢 2024-08-02 20:30:06

导入系统.Web
或者调用包含它的 System.Web.HttpUtility

如果尚不存在,您将需要添加对 DLL 的引用

string TestString = "This is a <Test String>.";
string EncodedString = System.Web.HttpUtility.HtmlEncode(TestString);

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);
假面具 2024-08-02 20:30:06

如果您使用 C#3,一个好的技巧是创建一个扩展方法以使其更加简单。 只需创建一个静态方法(最好在静态类中),如下所示:

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

然后您可以做如下简洁的事情:

string encoded = "<div>I need encoding</div>".HtmlEncode();

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();
儭儭莪哋寶赑 2024-08-02 20:30:06

尝试这个

System.Net.WebUtility.HtmlDecode(string);
System.Net.WebUtility.HtmlEncode(string);

Try this

System.Net.WebUtility.HtmlDecode(string);
System.Net.WebUtility.HtmlEncode(string);
舂唻埖巳落 2024-08-02 20:30:06

添加对System.Web.dll的引用,然后就可以使用System.Web.HtmlUtility类

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

小红帽 2024-08-02 20:30:06

只需引用 System.Web 程序集,然后调用:
HttpServerUtility.HtmlEncode

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

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

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

剩一世无双 2024-08-02 20:30:06

如果您使用的是 SharePoint 2010,使用以下代码行将避免引用整个 System.Web 库:

Microsoft.SharePoint.Utilities.SPHttpUtility.HtmlEncode(stringToEncode);

In case you're using SharePoint 2010, using the following line of code will avoid having to reference the whole System.Web library:

Microsoft.SharePoint.Utilities.SPHttpUtility.HtmlEncode(stringToEncode);
放赐 2024-08-02 20:30:06

如果您正在使用 silverlight,请使用以下命令:

System.Windows.Browser.HttpUtility.HtmlEncode(...);

In case you are working with silverlight, use this:

System.Windows.Browser.HttpUtility.HtmlEncode(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文