C# 如何对 URL 进行编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1461223/
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 do I encode an URL?
提问by
When I run my project I get the url http://localhost:5973/PageToPageValuePass/Default.aspx
I want to Encode the URL since sometimes I need to transfer data from page to page. When the urls are encoded then it increases the reliability.
当我运行我的项目时,我得到了 urlhttp://localhost:5973/PageToPageValuePass/Default.aspx
我想对 URL 进行编码,因为有时我需要在页面之间传输数据。当 url 被编码时,它会增加可靠性。
Server.UrlEncode("http://www.google.com/c#");
I get this, but how do I use it to help me encode the url?
我明白了,但是如何使用它来帮助我对 url 进行编码?
回答by Stuart Thompson
Url encoding is used to ensure that special symbols included in a url (most likely in a querystring) are not mistakenly interpreted as those used in the parsing and processing of a url. For example, the + symbol is used to indicate a space in a url. However, if you were intending for a + symbol to be a part of your querystring then you would want to encode that querystring before sending it to a browser.
Url 编码用于确保包含在 url 中的特殊符号(最有可能在查询字符串中)不会被错误地解释为在解析和处理 url 时使用的那些符号。例如,+ 符号用于表示 url 中的空格。但是,如果您打算将 + 符号作为查询字符串的一部分,那么您需要在将该查询字符串发送到浏览器之前对其进行编码。
For example. Imagine you have written a page that receives a math equation on the querystring and displays that equation on the page.
例如。假设您编写了一个页面,该页面接收查询字符串上的数学方程并在页面上显示该方程。
The url might be: http://yoursite.com/displayMath.aspx?equation=3+5
网址可能是: http://yoursite.com/displayMath.aspx?equation=3+5
The + symbol in this case is intended to be a meaningful part of the equation. However, without a UrlEncode it would be interpreted as representing a space. Reading this value from the querystring on the receiving page would yield "3 5", which is not what was intended.
在这种情况下,+ 符号旨在成为等式的一个有意义的部分。但是,如果没有 UrlEncode,它将被解释为表示一个空格。从接收页面上的查询字符串中读取此值将产生“3 5”,这不是预期的。
Instead of redirecting to that url directly, you would want to URL encode the request first. You might write the following code:
与其直接重定向到该 url,不如先对请求进行 URL 编码。您可能会编写以下代码:
string equation = "3+5";
string url = String.Format(@"http://yoursite.com/displayMath.aspx?equation={0}", equation);
string encodedUrl = Server.UrlEncode(url);
Response.Redirect(encodedUrl);
string equation = "3+5";
string url = String.Format(@"http://yoursite.com/displayMath.aspx?equation={0}", equation);
string encodedUrl = Server.UrlEncode(url);
Response.Redirect(encodedUrl);
This would ensure that a subsequent Request.Querystring["equation"]
would receive the equation intact because any special symbols would first be encoded.
这将确保后续Request.Querystring["equation"]
将完整地接收等式,因为任何特殊符号将首先被编码。
I'm not sure I understand your use case for encoding urls. If you could perhaps provide more information on what you are trying to achieve I will attempt to answer more fully. For now I hope that this information is useful.
我不确定我是否理解您对 url 进行编码的用例。如果您可以提供有关您要实现的目标的更多信息,我将尝试更全面地回答。现在我希望这些信息是有用的。
回答by csharptest.net
If your encoding parts of the path:
如果您对路径的编码部分:
System.Uri.EscapeUriString("c#")
If your encoding 'arguments':
如果您的编码“参数”:
String.Format( "http://something/?test={0}", System.Uri.EscapeDataString("c#") );
回答by TheVillageIdiot
say you want to create a link with some parameters you can use it as follows:
假设你想创建一个带有一些参数的链接,你可以按如下方式使用它:
aspx:
ASP:
Click Here
点击这里
code behind:
后面的代码:
myLink.Href = Page.ResolveClientUrl("~/MyPage.aspx") + "?id=" +
Server.UrlEncode("put here what ever you want to url encode");
Or as in your question:
或者如你的问题:
myLink.Href = "http://www.google.com/")+Server.UrlEncode("C#");
this will put in html:
这将放入 html:
<a id="myLink" runat="server" target="_self" href="http://www.google.com/c+c%23">
回答by RRUZ
try this
尝试这个
- in ASP.NET
- 在 ASP.NET 中
Server.UrlEncode("http://www.google.com/c#");
in WinForms using System.Web.dll
HttpUtility.UrlEncode("http://www.google.com/c#");
在 WinForms 中使用 System.Web.dll
HttpUtility.UrlEncode("http://www.google.com/c#");