2012-01-20 132 views
3

我传递一个加密的URL字符串:查询字符串转换为URL编码使用的NameValueCollection

Default.aspx的S3tLlnIKKzE%3D

我想传递一个URL字符串回的ASPX页面变量。

protected string qs = string.Empty; 

NameValueCollection qscollstring = HttpContext.Current.Request.QueryString; 
qs = qscollstring[0]; 

它会返回:S3tLlnIKKzE =

在qscollstring值[0]是正确的:S3tLlnIKKzE%3D

我理解这个问题是URL编码,但我不能找到一种方法来保持字符串原样。

似乎分配从qscollstring值[0]是:S3tLlnIKKzE%3D
到字符串改变了值:S3tLlnIKKzE =

我需要呆:S3tLlnIKKzE%3D

回答

4

使用HttpUtility.UrlEncode方法对字符串进行编码。

qs =HttpUtility.UrlEncode(qscollstring[0]); 
0

您还可以从当前URL的Uri中提取值,而无需对值进行编码。

样品:

Uri u = new Uri("http://localhost.com/default.aspx?S3tLlnIKKzE%3d"); 
string q = u.Query; 

和你的网页的一部分:

string q = !String.IsNullOrEmpty(Request.Url.Query) && Request.Url.Query.Length > 1 ? Request.Url.Query.Substring(1) : Request.Url.Query; 
0

像我一样,如果你正在寻找reverse ..使用

qs =HttpUtility.UrlDecode("S3tLlnIKKzE%3d"); 

找回S3tLlnIKKzE =

相关问题