以编程方式拉取生成的 HTML 与 HttpWebRequest
对于我们的时事通讯,我在网页中生成电子邮件的最终正文,然后希望将其拉入电子邮件正文中。我找到了一种使用 HttpWebRequest 来做到这一点的方法。
private string GetHtmlBody(Guid id)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("http://news.domain.com/News/View/{0}", id.ToString()));
HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(responce.GetResponseStream());
return sr.ReadToEnd();
}
但是,我觉得必须有更好的方法。我可以在不进行网络调用的情况下以某种方式提取生成的视图吗?
For our newsletter, I generate the final body of the email in a web page and then want to pull that into the body of the email. I found a way to do that with HttpWebRequest
.
private string GetHtmlBody(Guid id)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("http://news.domain.com/News/View/{0}", id.ToString()));
HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(responce.GetResponseStream());
return sr.ReadToEnd();
}
However, I feel there has to be a better way. Can I somehow pull the generated view without making a web call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 MVCContrib 来完成此任务 。
或者尝试编写一些丑陋的代码:
然后:
You could use MVCContrib for this task.
Or try to roll some ugly code:
and then:
假设电子邮件代码与网站位于同一项目中,那么您应该能够调用操作方法,获取 ActionResult 返回,然后调用 ExecuteResult 方法。缺点是,为了以这种方式执行此操作,您需要对其进行设置,以便 ExecuteResult 将写入您可以利用的流。为了完成所有这些,您需要模拟
ControllerContext
使用的一些类。更好的方法(尽管可能需要更多工作)可能是通过 XSLT 转换生成您想要的标记。 XSLT 是一种 XML 文档模板,可以与保存数据的 XML 文档合并以产生所需的结果。如果您这样做,那么您可以让发送电子邮件的流程运行转换,也可以让您的网站运行转换。这样做的优点是,如果您希望标记不同(即您正在重新设计新闻通讯),您只需更新 XSLT 文件并部署它即可。
华泰
Assuming the the email code is in the same project as the website, then you should be able to call the action method, get the ActionResult back, then call the ExecuteResult method. The downside is that in order to do it this way, you will need to set it up such that the ExecuteResult will write to a stream that you can take advantage of. In order to do all of this, you will need to mock up some of the classes used by the
ControllerContext
.What would probably be a better way (though will likely take more work), is to have the markup you want be generated by an XSLT transform. XSLT is a type of XML document template that can be merged with an XML document that holds data to produce a desired result. If you do this, then you can have your process that sends out emails run the transform as well as have your website run the transform. The advantage of this, is that if you want the markup to be different (i.e. you are redesigning thew newsletter), you will simply need to update the XSLT file and deploy it.
HTH
终于得到了一个可行的解决方案。在找到一些适当的搜索词(感谢@Darin)之后,我进行了很多很多试验
Finally got a working solution. After finding some proper search terms (thanks to @Darin) any many, many trials I found a solution that works. Putting this in my controller then passing the rendered string into my
EmailHelper
works great for what I needed.