asp.net 获取呈现的 HTML 的副本
我正在创建一个网站,该网站将提供通过电子邮件向用户发送网站最后一页上的动态、代码隐藏、计算驱动内容副本的选项。现在要发送电子邮件,当用户单击按钮时,我会再次从模型对象获取计算值,手动编写所有 HTML 标签,将模型数据粘贴在需要的位置。
我的问题是:有没有更简单的方法将网站输出复制到 html 格式的电子邮件?我目前手动编写 HTML 电子邮件,并且希望能够获取呈现的 HTML 的副本,并可能从那里对其进行修改。
I am creating 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法(至少对我来说)是实际创建一个网页来完成您想要它做的所有事情,然后使用 System.Net.WebClient 或 System.Net.HttpWebRequest 抓取它。
因此,您已经有了
Page1.aspx
,它允许人们选择一些内容并提交表单,然后您就得到了Email1.aspx
,这是最终的输出。当提交Page1.aspx
时,使用WebClient
下载Email1.aspx
的内容,如果需要,可能会传递一个查询字符串或一些 cookie。用户永远不会“看到”Email1.aspx
(当然除了他们的电子邮件),它只是在幕后使用。这种方法的优点是您可以测试 HTML 输出,而无需跳过 SMTP 环。在
Email1.aspx
页面中,如果您需要修改 HTML,您还可以重写OnRender()
方法(我认为就是这个方法,可能有不同的名称) 。或者您可以在下载后修改 HTML。如果您执行相同的基本 HTML 修改,您可以子类化System.Web.UI.Page
,实现您的自定义呈现,然后让所有电子邮件从您的新子类继承。需要记住的一件事很重要,沿着这条路线创建一个与用户请求完全独立的 HTTP 请求,因此会话和 cookie 等内容不会自动传递,您需要找到一种方法来自行完成此操作。
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
orSystem.Net.HttpWebRequest
.So you've got
Page1.aspx
which allows a person to select some things and submit a form and then you've gotEmail1.aspx
which is the final output. WhenPage1.aspx
is submitted useWebClient
to download the contents ofEmail1.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 theOnRender()
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 subclassSystem.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.
访问最终准备渲染的 html
您可以使用 asp.net方法
You can access the final ready-to-render html using the asp.net
method