How to freeze a dynamic aspx page into a static html page (on the server).
来源:互联网 发布:js页面提示信息 编辑:程序博客网 时间:2024/06/10 04:12
Take this scenario: On a website a visitor fills in a form after which the form is to be filed as a static html page. You might have several reasons for wanting to do that, one of them are search engines. Getting it done in asp.net took me more effort than I had originally expected. Let me share what I have found to be working.
The easy way to catch the html rendered by asp.net is to save a page in the client browser. To catch it on the server you have to hook into the (html) rendering process. There is no Page.OnRender event but the Page class does have a Render method. The method has a protected visibility, you cannot invoke it form your code. The Render method is invoked when asp.net renders the page to the output, to hook in you override the method. The Render method has a parameter of type HtnlTextWriter, the default implementation of the overriden Render method is invoking Base.Render passing it the HtmlTextWriter object.
protected override void Render(HtmlTextWriter writer)
{
// Default behavior
base.Render(writer);
}
This gives a place to hook in an own textwriter where asp,net can render the html into. The constructor of the HTMLtextwriter class has a protected visibility. Passing an own htmltextwriter requires specializing the frameworks's HTMLtextwriter class. The constructor of the base class requires a stringwriter. A wrapper class to house both custom HTMLtextwriter and stringwriter:
internal class MyHtmlFileCreator
{
private StringWriter html;
private MyHtmlTextWriter htmlWriter;
// override the HtmlTextWriter to reach the constructor
// the constructor in the base class is protected
class MyHtmlTextWriter : HtmlTextWriter
{
internal MyHtmlTextWriter(TextWriter tw): base(tw){}
}
// publish the HTMLwriter
internal HtmlTextWriter RenderHere
{
get {return htmlWriter;}
}
// constructor initializes stringwriter and htmlwriter based on that
// initialize Url
internal MyHtmlFileCreator()
{
html = new StringWriter();
htmlWriter = new MyHtmlTextWriter(html);
newUrl = Context.Request.Url.AbsolutePath.ToString();
newUrl = newUrl.Replace(".aspx",".htm");
}
internal void WriteHTMLFile(string virtualFileName)
{
// Stringreader reads output rendered by asp.net
// Stringwriter writes html output file
StringReader sr = new StringReader(html.ToString());
StringWriter sw = new StringWriter();
// Read from input
string htmlLine = sr.ReadLine();
while (htmlLine != null)
{
// Filter out ASp.net specific tags
if (! ((htmlLine.IndexOf("<form") > 0) ||
(htmlLine.IndexOf("__VIEWSTATE") > 0) ||
(htmlLine.IndexOf("</form>") > 0) ))
{sw.WriteLine(htmlLine);}
htmlLine = sr.ReadLine();
}
// Write contents stringwriter to html file
StreamWriter fs = new StreamWriter(virtualFileName);
fs.Write(sw.ToString());
fs.Close();
}
}
A MyHtmlFileCreator object has a place asp.net can render to and it has a method to write the contents of the stringwriter (inside the htmlwriter) to file. I use the MyHtmlFileCreator in a page base class. The page hooks in the render event. When the freeze flag is set the html is rendered to a file and the application is redirected to the html file just created.
public class FreezablePage : System.Web.UI.Page
{
internal class MyHtmlFileCreator{}
// When Asp.Net renders the page the Page.Render method is invoked
// Override the method to hook in
protected override void Render(HtmlTextWriter writer)
{
if (freeze)
{
MyHtmlFileCreator htmlFile = new MyHtmlFileCreator();
// Let Asp.net render the output, catch it in the file creator
base.Render(htmlFile.RenderHere);
// Write new html file
htmlFile.WriteHTMLFile(Server.MapPath(NewUrl));
// Redirect
Response.Redirect(NewUrl, true);
}
else
{
// Default behavior
base.Render(writer);
}
}
// Flag render event
protected void Freeze()
{
freeze = true;
}
protected void Freeze(string toUrl)
{
freeze = true;
NewUrl = toUrl;
}
private bool freeze = false;
private string newUrl;
internal string NewUrl
{
get
{
return newUrl;
}
set
{
newUrl = value;
}
}
}
}
Use this on your page like this :
public class _Default : Gekko.Web.UI.FreezablePage
{
private void Button1_Click(object sender, System.EventArgs e)
{
Freeze(string.Format(@"Statisch/{0}.htm", TextBox1.Text));
}
}
You will see that the resulting page has lost its form knocking out al buttons. There are no more postback possible. And the viewstate is also cut away. To process any file asp.net might produce will require more filtering. You can do quite flexible things treating the input as xml. Which requires some cutting and pasting, an html document is not always well-formed.
BTW Firefox was a lovely sidekick. The way it displays the html source of a page..mmm:>
Peter
- How to freeze a dynamic aspx page into a static html page (on the server).
- How to freeze a dynamic aspx page into a static html page (on the server).
- How to freeze a dynamic aspx page into a static html page (on the server).(转:http://codebetter.com/blogs/peter.van.ooijen/archiv
- Show how to print a table in a html page
- How to Capture the Row Selected on Search Page In a Variable?
- How to save a web page as HTML or MHT
- How to remove a block from the particular page?
- How to create a minimal master page
- How to Create A Facebook Page
- How to Make a Single Page Website
- How to include a header on each page when printing a DataGrid
- How to deploy a .Net assmebly for COM use through CAB on Web Page (转)
- How to deploy a .Net assmebly for COM use through CAB on Web Page
- Primefaces: How to set page on datatable after inserting a new row
- How to pass a value from User Control to the Page
- how to know wether an element is exist in a html page.
- How To Retrieve the URL of a Web Page from an ActiveX Control
- How To Print a Form That Is Too Large for the Screen or Page
- 从飞龙文章系统上传漏洞到系统管理员的飞跃
- ASP.NET中实现Web打印的方案
- Struts国际化问题解决
- 有关中国股市的好文章
- C# 2.0对现有语法的改进
- How to freeze a dynamic aspx page into a static html page (on the server).
- asp中对多余参数的完美过滤
- 好困惑
- ASP.NET 2.0 的内部变化
- .net中使用Random()产生一个随机数
- 水晶报表使用方法
- WinTasks Process Library (常用进程查询)
- 李开复致信中国大学生:大学4年应是这样度过
- 测试