asp.net 代码实现解析 aspx 代码的问题

清风道禅 2011-07-20 04:40:55
表达能力不好直接贴示例代码:
目标aspx的HTML如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<div>
<asp:Label ID="Label1" runat="server" Text="你好"></asp:Label>
</div>
</form>
</body>
</html>



经过asp.net框架解析后的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><title>

</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJOTczNTMyNjI5ZGRf/wLGKmD59ZxahaYwCsqwWGZffQ==" />
</div>


<div>
<span id="Label1">你好</span>
</div>
</form>
</body>
</html>




我想在CS代码中来自己实现这个转换,我输入进ASPX代码 程序给我输出解析后的HTML代码 请各位赐教·

...全文
312 66 打赏 收藏 转发到动态 举报
写回复
用AI写文章
66 条回复
切换为时间正序
请发表友善的回复…
发表回复
清风道禅 2011-07-27
  • 打赏
  • 举报
回复
结贴···谢谢大家参与·
清风道禅 2011-07-25
  • 打赏
  • 举报
回复
[Quote=引用 64 楼 sp1234 的回复:]
你可以在运行时不断动态修改 dynamic.ascx 文件中的内容。
[/Quote]
嗯···如果不包含From标记和HEAD 的服务器标记·是可以正确解析的·但是只要包含
<form id="form1" runat="server">

就会出错·· - -因为 <form id="form1" runat="server">
要求每个页面唯一 - -···
sxldfang 2011-07-22
  • 打赏
  • 举报
回复
给个链接:

http://download.csdn.net/source/3460176http://download.csdn.net/source/3460176
清风道禅 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 56 楼 sxldfang 的回复:]
Cassini(卡西尼)是asp.net上的一个开源项目。主要给出一个脱离IIS实现asp.net执行环境。项目演示了如何自己创建一个web server,并且运行一个asp.net应用程序。

网上有源码,找个研究一下,可增强功力!
[/Quote]
高手啊·真有?我去找找
  • 打赏
  • 举报
回复
你可以在运行时不断动态修改 dynamic.ascx 文件中的内容。
  • 打赏
  • 举报
回复
如果你只想要一部分主要的html代码,我给你写个小demo:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
EnableEventValidation="false" %>

<!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 runat="server">
<title>动态查看 ~/app_data/test/dynamic.ascx 输出的 html 源代码</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
using System;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
var uc = this.LoadControl("~/app_data/test/dynamic.ascx");
this.Form.Controls.Add(uc);
StringBuilder html = new StringBuilder();
var hw = new HtmlTextWriter(new StringWriter(html));
uc.RenderControl(hw);
this.Form.Controls.Remove(uc);
var label = new Label { Text = Server.HtmlEncode(html.ToString()) };
this.Form.Controls.Add(label);
}

public override void VerifyRenderingInServerForm(Control control)
{
}
}
清风道禅 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 58 楼 sxldfang 的回复:]
给个链接:

http://download.csdn.net/source/3460176http://download.csdn.net/source/3460176
[/Quote]
谢谢 正在看
清风道禅 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 59 楼 changlr01 的回复:]
你这样的解析,属于模板引擎的范畴,将静态页面解析输出,可以显示这样的效果

读取指定路劲静态文件,然后 用正则 匹配替换,用Response.Write输出即可。
[/Quote]
如果是替换就对了 - -
changlr01 2011-07-22
  • 打赏
  • 举报
回复
你这样的解析,属于模板引擎的范畴,将静态页面解析输出,可以显示这样的效果

读取指定路劲静态文件,然后 用正则 匹配替换,用Response.Write输出即可。
Rock870210 2011-07-21
  • 打赏
  • 举报
回复
楼主想实现这样的框架,还是先去学学如何创建自定义服务器控件吧。
段传涛 2011-07-21
  • 打赏
  • 举报
回复
16楼的方法你可以试试。
还不行就用控件。IHTMLDocument
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Net;

namespace AnExampleForNeuMik
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//举例
string url = "http://localhost:5352/Default.aspx";
string param = "p1=1&p2=lina";
Response.Write(GetResponseHTML(url,param));
}
protected string GetResponseHTML(string url,string param)
{
string htmlContent = string.Empty;
byte[] bs = System.Text.Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
StreamReader sr = new StreamReader(wr.GetResponseStream());
htmlContent=sr.ReadToEnd();
}
return htmlContent;
}
}
}
sxldfang 2011-07-21
  • 打赏
  • 举报
回复
Cassini(卡西尼)是asp.net上的一个开源项目。主要给出一个脱离IIS实现asp.net执行环境。项目演示了如何自己创建一个web server,并且运行一个asp.net应用程序。

网上有源码,找个研究一下,可增强功力!
孟子E章 2011-07-21
  • 打赏
  • 举报
回复
这样就可以了,重写Render方法

ASP.NET 中如何对生成的 HTML 内容流进行控制?


在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制,是页面的 render 方法中很容易实现这个功能。下面就是一个实现的方法,注释都在代码中。
ASPX 代码
<%@ Page Language="C#" %>

<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected override void Render(HtmlTextWriter writer)
{
string content = string.Empty;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
try
{
// 将当前页面的内容呈现到临时的 HtmlTextWriter 对象中
base.Render(htmlWriter);
htmlWriter.Close();

// 得到当前页面的全部内容
content = stringWriter.ToString();

// 替换页面中的部分内容
string newContent = content.Replace("[mxh]", "孟宪会");

// 将新页面的内容显示出来
writer.Write(newContent);
}
catch { }
finally
{
stringWriter.Dispose();
htmlWriter.Close();
htmlWriter.Dispose();
}
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>孟宪会之替换页面呈现内容测试</title>
</head>
<body>
<form id="form1" runat="server">
[mxh]
</form>
</body>
</html>
zyug 2011-07-21
  • 打赏
  • 举报
回复
补充一下,,源保存为一个abc.aspx之类的文件。
zyug 2011-07-21
  • 打赏
  • 举报
回复
呵呵。。。那很简单的呀,你先把你的源HTML保存为一个文件,
再用HttpRequest去请求这个文件,得到输出就可以了呀
清风道禅 2011-07-21
  • 打赏
  • 举报
回复
再顶一下·大家帮忙出出主意··
清风道禅 2011-07-21
  • 打赏
  • 举报
回复
我就纳闷了·既然微软能托管程序能运行·那么我们也应该能用代码来解析才对啊·
清风道禅 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wxr0323 的回复:]
可以在后台直接输出
<asp:Label ID="Label1" runat="server" Text="你好"></asp:Label>
这样的东西
然后给你自动生成。
[/Quote]

我的意思是想直接解析出来再输出到前台·调用框架来解析·而不是自己手工来做这些事情·
加载更多回复(46)

62,025

社区成员

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

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

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

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