C#方法做URL编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1506873/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:14:48  来源:igfitidea点击:

C# method to do URL encoding?

c#httpencodingcharacter-encoding

提问by Greg

what c# class method can I use to URL encode a URL string?

我可以使用什么 c# 类方法对 URL 字符串进行 URL 编码?

In my use case I want to pass a URL string as a URL parameter itself. So like burying a URL within a URL. Without some encoding the "&" and "?" characters in the inner URL can get picked up when the parameters for the outer Url parameters are processed

在我的用例中,我想将 URL 字符串作为 URL 参数本身传递。就像将 URL 埋在 URL 中一样。没有一些编码“&”和“?” 当处理外部 URL 参数的参数时,可以拾取内部 URL 中的字符

thanks

谢谢

回答by Fredrik M?rk

HttpServerUtility.UrlEncodemight be exactly what you are looking for.

HttpServerUtility.UrlEncode可能正是您正在寻找的。

回答by Linda Jobs

I am operating with .net framework 2.0. But it never shows me "UrlEncode" intelisense when I place dot after httpserverutility. Here are the options I see:

我正在使用 .net framework 2.0。但是当我在 httpserverutility 之后放置点时,它永远不会向我显示“UrlEncode”智能感知。以下是我看到的选项:

HttpServerUtility.Equals 
HttpServerUtility.ReferenceEquals
HttpServerUtility.UrlTokenDecode
HttpServerUtility.UrlTokenEncode

That's it, these are the only options I see. Could I possibly ask for small piece of code as an example to use url encode function?

就是这样,这些是我看到的唯一选择。我可以要求一小段代码作为使用url编码功能的例子吗?

Thanks,

谢谢,

Linda

琳达

回答by tematin

Use Server.UrlEncode

使用 Server.UrlEncode

You need an instance of the HttpServerUtility class, because the UrlEncode method is not static.

您需要 HttpServerUtility 类的实例,因为 UrlEncode 方法不是静态的。

See http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx

请参阅http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx

回答by Ivan Lewis

System.Net.WebUtility.UrlEncode(str)