以编程方式拉取生成的 HTML 与 HttpWebRequest

发布于 2024-10-13 11:49:45 字数 554 浏览 4 评论 0原文

对于我们的时事通讯,我在网页中生成电子邮件的最终正文,然后希望将其拉入电子邮件正文中。我找到了一种使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情栀口红 2024-10-20 11:49:45

您可以使用 MVCContrib 来完成此任务

或者尝试编写一些丑陋的代码:

public static string ViewToString(string controlName, object viewData)
{
    var vd = new ViewDataDictionary(viewData);
    var vp = new ViewPage { ViewData = vd };
    var control = vp.LoadControl(controlName);
    vp.Controls.Add(control);
    var sb = new StringBuilder();
    using (var sw = new StringWriter(sb))
    using (HtmlTextWriter tw = new HtmlTextWriter(sw))
    {
        vp.RenderControl(tw);
    }
    return sb.ToString();
}

然后:

var viewModel = ...
string template = ViewToString("~/Emails/EmailTemplate.ascx", viewModel);

You could use MVCContrib for this task.

Or try to roll some ugly code:

public static string ViewToString(string controlName, object viewData)
{
    var vd = new ViewDataDictionary(viewData);
    var vp = new ViewPage { ViewData = vd };
    var control = vp.LoadControl(controlName);
    vp.Controls.Add(control);
    var sb = new StringBuilder();
    using (var sw = new StringWriter(sb))
    using (HtmlTextWriter tw = new HtmlTextWriter(sw))
    {
        vp.RenderControl(tw);
    }
    return sb.ToString();
}

and then:

var viewModel = ...
string template = ViewToString("~/Emails/EmailTemplate.ascx", viewModel);
风吹过旳痕迹 2024-10-20 11:49:45

假设电子邮件代码与网站位于同一项目中,那么您应该能够调用操作方法,获取 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

紫﹏色ふ单纯 2024-10-20 11:49:45

终于得到了一个可行的解决方案。在找到一些适当的搜索词(感谢@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文