Creating ASP.NET MVC style views in a console application?
Asked Answered
F

6

5

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.

Footy answered 19/10, 2010 at 16:3 Comment(1)
This seems to be a related (and very similar) question: https://mcmap.net/q/848728/-asp-net-mvc-email.Thompson
T
3

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).

Thompson answered 19/10, 2010 at 16:43 Comment(1)
Thanks, I can't believe I missed that. Spark looks 100% easier than Razor to setup and use as a standalone templating solution. That may change in the future but this is definitely more mature.Footy
C
4

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/

Cassowary answered 19/10, 2010 at 16:13 Comment(2)
This is good - and there wos supposed to be more native support for this in later versions of MVC 3 / Razor.Daughter
Another implementation: buildstarted.com/2010/11/02/razor-without-mvc-part-iiNolita
T
3

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).

Thompson answered 19/10, 2010 at 16:43 Comment(1)
Thanks, I can't believe I missed that. Spark looks 100% easier than Razor to setup and use as a standalone templating solution. That may change in the future but this is definitely more mature.Footy
C
2

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

Contessacontest answered 19/10, 2010 at 16:17 Comment(0)
R
0

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.

Radiochemical answered 19/10, 2010 at 16:8 Comment(0)
H
0

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>
Holeproof answered 19/10, 2010 at 16:37 Comment(2)
The main downside of this approach is that view engines already have concepts like looping and conditionals baked into the syntax. So you could "bind" to a list list of CLR objects representing job listings and in your view specify what markup to use. Your approach looks like it would require generating some HTML markup in code which creates fairly strong coupling between layout of the email and the code used to populate it with data.Thompson
Yes, it does require the generation of some html, it just an improved version of what the OP is doing now.Holeproof
R
0

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.

Raze answered 19/10, 2010 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.