开发者

asp.net obtain copy of rendered HTML

I am creati开发者_如何学Cng a website that will offer the option to email the user a copy of dynamic, code-behind, calculation-driven content on a final page of a website. To send an email as of now I obtain the calculation values from the model object again when the user clicks the button, writing all HTML tags by hand, sticking the model data in where needed.

My question is: is there any easier way to copy website output to an html formatted email? I currently code an HTML email by hand and would like the ability to just get a copy of the rendered HTML, and possible modify it from there.


The easiest way (at least for me) is to actually create a webpage that does everything that you want it to do and then scrape it using System.Net.WebClient or System.Net.HttpWebRequest.

So you've got Page1.aspx which allows a person to select some things and submit a form and then you've got Email1.aspx which is the final output. When Page1.aspx is submitted use WebClient to download the contents of Email1.aspx, probably passing a querystring or some cookies over if needed. The user never "sees" Email1.aspx (except in their email of course), its just used behind the scenes.

The advantage of this approach is that you can test the HTML output without having to jump through SMTP hoops. In the Email1.aspx page you could also override the OnRender() method (I think that's the one, might have a different name possibly) if you need to modify the HTML. Or you could modify the HTML after downloading it. If you're performing the same basic HTML modifications you could subclass System.Web.UI.Page, implement your custom rendering and then have all of the emails inherit from your new subclass.

One thing that's important to remember, going down this route creates an HTTP request that's completely separate from the user's request, so things like session and cookies aren't passed over automatically, you'll need to find a way to do that on your own.


You can access the final ready-to-render html using the asp.net

Response.WriteFile("filename"); 

method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜