动态页面静态化,静态页面不能预览

XUXU_dragon 2009-09-16 11:20:02

public static bool WriteFile(string strText, string strContent, string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("Html/");
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");

// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("Html/ActionDetails.html");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
// 替换内容 // 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$title$", strText); //模板页中的ShowArticle
str = str.Replace("$time$", strText);
str = str.Replace("$content$", strContent);
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename, false, code); //为false时候是覆盖,true为追加
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}




结果发现生成的Html代码是这样的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文?/title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div class="xxl_moban" id="content_xxl">
<div id="txt_xxl">
<h2>了解我们最新的印刷工艺与产品生产过程中的解决方案…?/h2>
<h1>? titititititititle</h1>
<p>?time$?/p>
<h3 class="conent">datatadatadatadatadata</h3>
</div>
</div>
</body>
</html
>

很多标签都变成了“?”
...全文
151 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
diffmaker 2009-09-17
  • 打赏
  • 举报
回复

public static bool WriteFile(string strText, string strContent, string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("Html/");
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");

// 读取模板文件

............



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


上面是GB2312,下面是UTF8,会是这个问题吗?楼主把编码统一一下试试吧
CODE163 2009-09-16
  • 打赏
  • 举报
回复
编码问题
gdjlc 2009-09-16
  • 打赏
  • 举报
回复


System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");

改成下面试下:
System.Text.Encoding code = System.Text.Encoding.GetEncoding("utf8");
XUXU_dragon 2009-09-16
  • 打赏
  • 举报
回复
自己顶一下!等高人来解决
XUXU_dragon 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 enihs 的回复:]
浏览器的 查看——〉字符编码方式是不是也是utf-8
[/Quote]

看了,是Unicode(utf-8)

我想应该不是浏览器的问题吧,
有很多标签都变成了“?”,咋回事呢?
enihs 2009-09-16
  • 打赏
  • 举报
回复
浏览器的 查看——〉字符编码方式是不是也是utf-8
XUXU_dragon 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 diffmaker 的回复:]
你查检一下传入的string strText, string strContent, string strAuthor的值是否正常吧
[/Quote]

为了方便调式,我是这么引用的
WriteFile("titititititititle", "datatadatadatadatadata", "contentcontentcontent");

传入的值没有问题。
diffmaker 2009-09-16
  • 打赏
  • 举报
回复
你查检一下传入的string strText, string strContent, string strAuthor的值是否正常吧
diffmaker 2009-09-16
  • 打赏
  • 举报
回复
有这种事儿?
XUXU_dragon 2009-09-16
  • 打赏
  • 举报
回复
这是模板页

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div class="xxl_moban" id="content_xxl">
<div id="txt_xxl">
<h2>了解我们最新的印刷工艺与产品生产过程中的解决方案……</h2>
<h1>◎ $title$</h1>
<p>【$time$】</p>
<h3 class="conent">$content$</h3>
</div>
</div>
</body>
</html>
jerry_zuo 2009-09-16
  • 打赏
  • 举报
回复
编码问题。
jdhlowforever 2009-09-16
  • 打赏
  • 举报
回复
学习~~~~
jhdxhj 2009-09-16
  • 打赏
  • 举报
回复
ding
LutzMark 2009-09-16
  • 打赏
  • 举报
回复
debug 单步执行

监视str
wuyq11 2009-09-16
  • 打赏
  • 举报
回复
或html页面改成
<META http-equiv=Content-Type content= "text/html; charset=gb2312 ">
看看
wuyq11 2009-09-16
  • 打赏
  • 举报
回复
换成System.Text.Encoding.GetEncoding("utf-8");编码
调试看看str 中内容。
屌丝女士111 2009-09-16
  • 打赏
  • 举报
回复
编码 看看.

xiongzhou8890 2009-09-16
  • 打赏
  • 举报
回复
d

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧