来自类库的 HtmlEncode
我有一个类库(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
System.Net.WebUtility
类是从 .NET Framework 4.0 开始可用 - 您不需要 System.Web 依赖项。
System.Net.WebUtility
class isavailable starting from .NET Framework 4.0 — you donʼt need System.Web dependency.
导入系统.Web
或者调用包含它的 System.Web.HttpUtility
如果尚不存在,您将需要添加对 DLL 的引用
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
如果您使用 C#3,一个好的技巧是创建一个扩展方法以使其更加简单。 只需创建一个静态方法(最好在静态类中),如下所示:
然后您可以做如下简洁的事情:
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:
You can then do neat stuff like this:
尝试这个
Try this
添加对System.Web.dll的引用,然后就可以使用System.Web.HtmlUtility类
Add a reference to System.Web.dll and then you can use the System.Web.HtmlUtility class
只需引用 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
如果您使用的是 SharePoint 2010,使用以下代码行将避免引用整个 System.Web 库:
In case you're using SharePoint 2010, using the following line of code will avoid having to reference the whole System.Web library:
如果您正在使用 silverlight,请使用以下命令:
In case you are working with silverlight, use this: