如何在 Visual Studio (vb.net) 中使用 HtmlEncode(或 HtmlDecode)函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8597891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use the HtmlEncode (or HtmlDecode) function in Visual Studio (vb.net)?
提问by Austin Burk
I'm making an application that involves logging into a server, however, the post data needs some encoding.
我正在制作一个涉及登录服务器的应用程序,但是,发布数据需要一些编码。
Dim strEncUsername As String = Server.HtmlEncode(Me.txtUsername.Text)
However, since this isn't an asp.net application, this doesn't work. How the hay am I supposed to do this? I tried looking for an Imports thing, but no real luck.
但是,因为这不是一个 asp.net 应用程序,所以这不起作用。我该怎么做呢?我试图寻找进口的东西,但没有真正的运气。
回答by davidsleeps
If you add a reference to System.Web to your project you can use the following to html encode your string
如果您将 System.Web 的引用添加到您的项目中,您可以使用以下内容对您的字符串进行 html 编码
Dim strEncUsername As String = System.Web.HttpUtility.HtmlEncode(Me.txtUsername.Text)
MSDN Documentation for HttpUtility.HtmlEncode
HttpUtility.HtmlEncode 的 MSDN 文档
Edit
Screenshot of intellisense showing HtmlEncode:
编辑
显示 HtmlEncode 的智能感知屏幕截图:
Screenshot of references in project:
项目中引用的截图:
Output from application:
应用程序的输出:
unsafe text: <em>evil em tags within</em>
safe text: <em>evil em tags within</em>
回答by Zibri
System.web is not available in NET 4.0 client profile... I wonder why.
System.web 在 NET 4.0 客户端配置文件中不可用......我想知道为什么。
It can't even be added as a reference.
它甚至不能作为参考添加。
But the same thing can be accomplished by using:
但是同样的事情可以通过使用来完成:
System.Net.WebUtility.HtmlDecode
System.Net.WebUtility.HtmlDecode
回答by Matej
You shoud use HttpUtility
(add reference to System.Web first).
您应该使用HttpUtility
(首先添加对 System.Web 的引用)。
回答by MyNameHere
In a Windows application you can also use:
在 Windows 应用程序中,您还可以使用:
System.Net.WebUtility.HtmlEncode
providing the Framework version is 4 or above, see: [link]https://msdn.microsoft.com/en-us/library/ee388364(v=vs.110).aspx
提供Framework版本为4或以上,见:[link] https://msdn.microsoft.com/en-us/library/ee388364(v=vs.110).aspx