运行我的项目后,我得到了URL:http://localhost:5973/PageToPageValuePass/Default.aspx
。我想要对URL进行编码,因为有时我需要在页面之间传递数据。当URL被编码时,它可以增加可靠性。
Server.UrlEncode("http://www.google.com/c#");
我理解这点,但如何使用它来帮助我编码URL?
运行我的项目后,我得到了URL:http://localhost:5973/PageToPageValuePass/Default.aspx
。我想要对URL进行编码,因为有时我需要在页面之间传递数据。当URL被编码时,它可以增加可靠性。
Server.UrlEncode("http://www.google.com/c#");
我理解这点,但如何使用它来帮助我编码URL?
System.Uri.EscapeUriString("c#")
如果您的编码参数为“arguments”:String.Format( "http://something/?test={0}", System.Uri.EscapeDataString("c#") );
.UrlEncode(...)
对于带有主题或正文的mailto
href
很糟糕。+1 - it3xl试一下这个
Server.UrlEncode("http://www.google.com/c#");
in WinForms using System.Web.dll
HttpUtility.UrlEncode("http://www.google.com/c#");
http://yoursite.com/displayMath.aspx?equation=3+5
在这种情况下,+符号旨在成为方程式的有意义的一部分。但是,如果没有UrlEncode,则会将其解释为表示空格。从接收页面的查询字符串读取此值将产生“3 5”,这不是预期的结果。string equation = "3+5";
string url = String.Format(@"http://yoursite.com/displayMath.aspx?equation={0}", equation);
string encodedUrl = Server.UrlEncode(url);
Response.Redirect(encodedUrl);
这将确保随后的Request.Querystring ["equation"]
将完整地接收方程式,因为任何特殊符号都将首先被编码。假设你想要创建一个带有一些参数的链接,你可以按照以下方式使用它:
aspx:
点击这里
后台代码:
myLink.Href = Page.ResolveClientUrl("~/MyPage.aspx") + "?id=" +
Server.UrlEncode("put here what ever you want to url encode");
或者像你的问题中一样:
myLink.Href = "http://www.google.com/")+Server.UrlEncode("C#");
这将放入HTML:
<a id="myLink" runat="server" target="_self" href="http://www.google.com/c+c%23">