URL编码解码

          string menuwaa = string.Empty;

        //string menupath = HttpContext.Current.Request.Url.ToString();

        //string menuwaa = menupath.Substring(0, menupath.LastIndexOf('/')+1);


        menuwaa += "About.aspx?user=";

        string aa = " and UserJobID in('5') ";         

        string heihei = Server.UrlPathEncode(aa);   

         Server.Transfer(menuwaa + heihei); 


接受

string get = Request["user"].ToString();     

   string linshi = HttpUtility.UrlDecode(get, System.Text.Encoding.UTF8);


当然毕竟是UrlPathEncode 可以对http://这样的内容也统一进行编码。 如果单纯的编码,也可以使用server.urlencode


注入前台。



FineUI.PageContext.RegisterStartupScript("zhurukey(\"" + Server.UrlPathEncode(Server.UrlPathEncode(htmltext)) + "\");");

前台接受:


 function zhurukey(option) {
            var string = decodeURIComponent(decodeURIComponent(option));
            bandingkey(string);
        }



还有一种:

 string aa = "UserJobID in('5')";

        string heihei1 = Server.UrlEncode(aa);

 Page.ClientScript.RegisterStartupScript(this.GetType(), "search", "<script>window.open('About.aspx?user=" + heihei1 + "','_blank')</script>");

后台打开即可。不需要做转码。



注意异常:


如果多个 url=“ 编码(里面多个等号)”

因为里面的等号都被编码了。所以用户得到的url 将是一个整个的 字符串,字符串中有多个等号。而无法通过requet直接获得。


另外。 如果

url=“ test=1&param=2”

<script>window.open('About.aspx?" + url + "','_blank')</script>


这种形式是对的。


但是如果要对其编码必须保证是对某一个等号后面的值进行编码。 因为编码会把等号改变成特殊符号。

将使得URL参数传递成乱码。


错误:

url=编码(“ test=1&param=2”)

<script>window.open('About.aspx?" + url + "','_blank')</script>





具体细节见:

解码转码---System.web.HttpUtility 对象分析



你可能感兴趣的:(URL编码解码)