注册 登录
编程论坛 ASP.NET技术论坛

【求助】在地址栏输入汉字后在后台获取显示乱码!

bshao0001 发布于 2009-11-12 11:55, 1295 次点击
只有本站会员才能查看附件,请 登录

程序代码:
  Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                Request.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                searchvalue = NewRay.utils.RequestClass.GetQueryString("searchvalue").Trim();
                searchvalue = HttpUtility.UrlDecode(searchvalue, System.Text.Encoding.UTF8);
2 回复
#2
bygg2009-11-12 12:24
UrlEncode
#3
bshao00012009-11-14 09:10
不过我想了一个解决办法:
1.创建一个HttpModule 类在此类中进行Url重写:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
namespace NewRay.Web.HttpModule
{
    public class DecodeModule:IHttpModule
    {
        #region IHttpModule 成员

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            string str = context.Request.RawUrl;
            int eqindex = str.LastIndexOf('=');
            int sv = str.IndexOf("searchvalue");
            if (eqindex > 0 && sv > 0)
            {
                string nowstr = str.Substring(eqindex + 1);
                if (!"".Equals(nowstr) && nowstr != null)
                {
                    Regex reg = new Regex("^[\u4e00-\u9fa5]{0,}$");

                    if (reg.IsMatch(nowstr))
                    {

                        string schineser = HttpUtility.UrlEncode(nowstr);

                        context.RewritePath("/searchlist.aspx?typevalue=product&pctype=tese&searchvalue=" + schineser + "");
                    }
                }
            }
        }

        #endregion
    }
}
2.在后台进行解码:
string str= HttpUtility.UrlDecode(strvalue);
注:在ie内核的好用。
1