在控制台应用程序中创建 ASP.NET MVC 样式视图?

发布于 2024-09-28 03:32:44 字数 1196 浏览 11 评论 0原文

我有一个控制台应用程序,要求我发送电子邮件。现在我使用字符串生成器来创建电子邮件,但我想变得更奇特。然后我突然意识到:最好将我的对象发送到 ASP.NET MVC 样式视图,其中我有 HTML 标记,然后将其返回以邮寄。现在,我的情况是:

    private void MailJobList(List<Job> newJobs) {
                var body = new System.Text.StringBuilder();
                var from = new MailAddress("[email protected]");
                var to = new MailAddress(addresslist.Get());

                var message = new MailMessage(from, to);

                message.Subject = "New job list";

    //mail settings ommitted here for brevity

                body.Append("New jobs: ");
                if (newJobs.Any()) {
                    foreach (var newJob in newJobs) {
                        body.Append(newJob.Job + ", ");
                    }
                }

                message.Body = body.ToString();

                client.Send(message);
}

显然,这只是纯文本,但我真的希望能够做类似的事情:

var body = RenderHTMLMessage(newJobs);

看起来我应该能够利用 ASP.NET MVC 的视图引擎(或 Spark 或任何其他视图引擎),而不是自己推出。如果我在这里偏离主题或者有任何更简单的方法可以做到这一点,我愿意接受建议。

I have a console application that requires me to send out e-mails. Right now I use a string builder to create the e-mails, but I'd like to get more fancy. Then it dawned on me: it would be nice to send my object to an ASP.NET MVC style view, where I'd have the HTML markup, and then return it to mail out. Right now, I have it going as;

    private void MailJobList(List<Job> newJobs) {
                var body = new System.Text.StringBuilder();
                var from = new MailAddress("[email protected]");
                var to = new MailAddress(addresslist.Get());

                var message = new MailMessage(from, to);

                message.Subject = "New job list";

    //mail settings ommitted here for brevity

                body.Append("New jobs: ");
                if (newJobs.Any()) {
                    foreach (var newJob in newJobs) {
                        body.Append(newJob.Job + ", ");
                    }
                }

                message.Body = body.ToString();

                client.Send(message);
}

Obviously that's just plain text, but I'd really like to be able to do something like:

var body = RenderHTMLMessage(newJobs);

It seems like I should be able to leverage ASP.NET MVC's view engine (or Spark or any other view engine) and not roll my own. If I'm off mark here or there's any easier way to do this, I'm open to suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

纵情客 2024-10-05 03:32:44

您可以在以下位置使用新的 Razor 视图引擎:控制台应用程序,请参阅以下博客文章:

http://thegsharp.wordpress.com/2010/07/07/using-razor-from-a-console-application/

You can use the new Razor view engine in a console app, see the following blog post:

http://thegsharp.wordpress.com/2010/07/07/using-razor-from-a-console-application/

深海蓝天 2024-10-05 03:32:44

您可以使用 Spark View Engine 作为 通用模板引擎。 Spark 的创建者写了一篇关于如何去做的博文(将是一个好的开始)。

You can use the Spark View Engine as a general purpose templating engine. The creator of Spark wrote a blogpost on how to go about doing it (would be a good start).

恏ㄋ傷疤忘ㄋ疼 2024-10-05 03:32:44

您可以使用 T4 模板(其语法类似于 asp.net)来执行此操作。不过,它需要 VS2010 附带的 T4 版本。 这里是一个示例,这里是关于该主题的msdn

You can use T4 templates, which have a syntax similar to asp.net, to do this. It requires the T4 version that ships with VS2010, though. Here is an example, and here is msdn on the subject

过气美图社 2024-10-05 03:32:44

这里的问题是 ASP.NET MVC 严重依赖于 ASP.NET,而 ASP.NET 又依赖于 Web 服务器。我认为这不可能按照您想要的方式实现。您可以做的就是自己托管 ASP.NET,并向自己发送虚假请求。

The problem here is that ASP.NET MVC relies heavily on ASP.NET, and ASP.NET relies on a webserver.. I don't think it's doable the way you want to. What you could do is to host ASP.NET yourself, and fake requests to yourself.

朱染 2024-10-05 03:32:44

MVC 基础设施对于这项任务来说太重了。除非您编写/托管 MVC 应用程序来创建页面,然后在发送电子邮件之前直接从 URL 读取 html(这也可能意味着(在浏览器中查看此)链接已创建(如果将其用于新闻通讯)。

对于特定电子邮件,创建包含所有 html(以及电子邮件所需的内联样式)的 html 模板文件,然后读取 html 并替换标记,例如 ##TO_NAME## 等标记或列表(写出的行)将是每个标记或列表的非常具体的代码无论如何,

这意味着您可以将电子邮件模板单独更改为代码并通过不删除电子邮件模板中的令牌来省略内容,

例如:

<html>
<body style="font-size:10px">
Dear ##To_NAME##< /br>
</br>
Your Jobs< /br>
  <table>
      <tr>
         <td colspan="2">New Jobs<td>
      </tr>
      ##JOB_LIST##
  <table>
  ##FROM_NAME##
</body>
</html>

The MVC infrastructure would be too heavy for this task. unless you write/host an MVC application to create the pages then read html directly from the URL before emailing (this could also mean that the (view this in the browser) link is already created (if using this for newletters.

For specific emails, create html template files that contain all the html (and inline styles required by emails), then reads in the html and replace the tokens eg ##TO_NAME## etc The tokens or lists (written out rows) will be pretty specific code to each one anyway.

This means you can change the email templates separatly to the code and ommit content by not removing the tokens from in the email template.

eg:

<html>
<body style="font-size:10px">
Dear ##To_NAME##< /br>
</br>
Your Jobs< /br>
  <table>
      <tr>
         <td colspan="2">New Jobs<td>
      </tr>
      ##JOB_LIST##
  <table>
  ##FROM_NAME##
</body>
</html>
月光色 2024-10-05 03:32:44

尝试一下 DotLiquid (www.dotliquidmarkup.org)。它是一个模板引擎,可用于各种应用程序,且语法简单。

Give a try to DotLiquid (www.dotliquidmarkup.org). It's a templating engine that can be used in every kind of application, with an easy syntax.

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