日期:2014-05-18  浏览次数:20537 次

动态页面静态化后的字符问题
我写了一个基类
C# code

public class BasePage:System.Web.UI.Page
{
    public BasePage()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        string name = Request.Url.AbsolutePath.Substring(1, Request.Url.AbsolutePath.Length - 1).Replace("aspx", "htm");
        string newurl = "";
        int num1 = name.LastIndexOf('/')+1;
        int num2 = name.Length-num1;
        string name1 = name.Substring(num1, num2);
        if (name1.IndexOf("/") < 0)
        {
            //newurl = Server.MapPath(name);
            //newurl = "/AssetWeb/"+name;
            newurl = Server.MapPath("./") + name1;
        }
        else
        {
            newurl = Server.MapPath("./") + name1;
        }
        MemoryStream ms = new MemoryStream();
        StreamWriter sww = new StreamWriter(ms);
        StreamWriter swr = new StreamWriter(newurl);
        System.Web.UI.HtmlTextWriter htmlw = new HtmlTextWriter(swr);
        base.Render(htmlw);
        htmlw.Flush();
        htmlw.Close();
        string strLL = System.Text.Encoding.UTF32.GetString(ms.ToArray());
        Response.Write(strLL);
        Response.Redirect(Request.Url.AbsoluteUri.Replace("aspx", "htm"), true);
    } 
}


但是页面显示后都是乱码,包括服务器控件也是,想请教下有什么解决的办法?

------解决方案--------------------
System.Text.Encoding.UTF32
改成System.Text.Encoding.Default试试
------解决方案--------------------
楼主你的动态页面静态化是啥子意思来着呢.

我看你的思路好像不太对,你仅把扩展名aspx替换个htm没啥用喔??

你在服务器端生成html了吗?怎样生成的呢?生成了直接访问htm就行了....

你先打开生成的html有没有乱码,如果有就是生成的问题....


------解决方案--------------------
==========================贴下我的代码==========================
string BoardStream = " ";//下载内容存入此变量

Uri url = new Uri( "http://www.hpday.com ");//将下载地址转换为Uri类型

HttpWebRequest requestPage = (HttpWebRequest)WebRequest.Create(url);

WebResponse response = requestPage.GetResponse();

Stream stream = response.GetResponseStream();//获取页面流

if (response.ContentType.ToLower().StartsWith( "text/ "))//如果获得成功(即为文本格式)
{

StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding( "gb2312 "));//读取获得内容流 gb2312
//StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);//读取获得内容流 utf8格式

BoardStream = reader.ReadToEnd();//将内容流转换为文本并存入变量BoardStream,即为所需要的数据流
}

StreamWriter saveAPage = new StreamWriter(Server.MapPath( "a.htm "), false, System.Text.Encoding.UTF8);//实例化写入类
saveAPage.Write(BoardStream);//创建写入任务
saveAPage.Flush();//写入文件(即清理缓存流)
saveAPage.Close();//关闭写入类的对象