js中escape对应的C#解码函数 UrlDecode
需要注意的几点:
1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。
2、Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。
3、用HttpUtility.UrlEncode编码后的字符串和用Server.UrlEncode进行编码后的字符串对象不一样:
例如:
string url="http://search.99read.com/index.aspx?book_search=all&main_str=奥迷尔";
Response.Write(HttpUtility.UrlEncode(url));
Response.Write("<br>");
Response.Write(Server.UrlEncode(url));
输出结果是:
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%e5%a5%a5%e8%bf%b7%e5%b0%94
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb
原因:Server.UrlEncode的编码方式是按照本地程序设置的编码方式进行编码的,而HttpUtility.UrlEncode是默认的按照.net的utf-8格式进行编码的。
string url1="http://search.99read.com/index.aspx?book_search=all&main_str=奥迷尔"; Response.Write(HttpUtility.UrlEncode(url1,System.Text.Encoding.GetEncoding("GB2312"))); Response.Write("<br>"); Response.Write(Server.UrlEncode(url1));
输出的结果是:
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb
http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb
3、有时候可能别的系统传递过来的url是用别的编码方式编码的。
介绍自己编写的一个方法,可以获取指定编码格式的QueryString。
public string GetNonNullQueryString(string key,Encoding encoding) { //引用System.Collections.Specialized和System.Text命名空间 string stringValue; System.Collections.Specialized.NameValueCollection encodingQueryString; //该方法是在2.0中新增的 encodingQueryString = HttpUtility.ParseQueryString(Request.Url.Query,encoding); //'里面的key就是你提交的参数的Key return encodingQueryString[key] != null ? encodingQueryString[key].Trim() : ""; }
调用:
string url = GetNonNullQueryString("url",Encoding.UTF8).Trim();
----------------------------------------------------------------------------------------------
javascript中escape,encodeURI,encodeURIComponent三个函数的区别
js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,
相应3个解码函数:unescape,decodeURI,decodeURIComponent
1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。
例如:
<script language="javascript">
document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');
</script>
2、 进行url跳转时可以整体使用encodeURI
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");
3、 js使用数据时可以使用escape
例如:搜藏中history纪录。
4、 escape对0-255以外的unicode值进行编码时输出%u****格式,
其它情况下escape,encodeURI,encodeURIComponent编码结果相同。
最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,
所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2015-09-11 启动与关闭comcat 命令