ASP.NET给ASHX文件传递中文参数

需要用到下面两个函数,具体是哪位大侠先写的就不知道了。先谢谢。

 

        /// <summary>
        /// 用来给ASHX传递的参数进行编码
         /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Escape(string str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                sb.Append((Char.IsLetterOrDigit(c)
                    || c == '-' || c == '_' || c == '\\'
                    || c == '/' || c == '.') ? c.ToString() : Uri.HexEscape(c));
            }
            return sb.ToString();
        }


        /// <summary>
        /// 用来解码Escape编码后的内容
         /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UnEscape(string str)
        {
            StringBuilder sb = new StringBuilder();
            int len = str.Length;
            int i = 0;
            while (i != len)
            {
                if (Uri.IsHexEncoding(str, i))
                    sb.Append(Uri.HexUnescape(str, ref i));
                else
                    sb.Append(str[i++]);
            }
            return sb.ToString();
        }


在ASPX页面的.cs文件里

        protected void Page_Load(object sender, EventArgs e)
        {
           ddlyxlx.Items.FindByValue(Request["Ddh"] == null ? "0" : CommandInfo.UnEscape(Server.UrlDecode(Request["Ddh"].ToString()))).Selected = true;
        }


        protected void btn_Search_Click(object sender, EventArgs e)
        {
            string _Url = string.Format("Check_TableDestineYanXi.aspx?StartDate={0}&EndDate={1}&CurrentDateNav={2}&Ddh={3}", txtStartDate.Text, txtEndDate.Text, DateNavSelect1.GetCurrentDateNav, CommandInfo.Escape(Server.UrlEncode(ddlyxlx.SelectedValue)));
            Response.Redirect(GetUrl());
        }



Ashx文件里面接收方法

string Ddh = (context.Request["Ddh"] == null ? "" : System.Web.HttpContext.Current.Server.UrlDecode(CommandInfo.UnEscape(context.Request["Ddh"])));

即可接收到中文的参数,不用修改web.config文件。

 

 

你可能感兴趣的:(c,String,object,null,search,asp.net)