views:

196

answers:

3

ASP.NET is great for creating html. We send out a lot of html email messages. In the past we've been loading .html templates with key strings that are replaced to make the email more custom (ie. [[FirstName]] would be replaced with "John Doe"). However as we are adding more and more email campaigns the logic to customize the email is starting to get out of hand. Instead of a simple [[FirstName]] string replace we are tasked with something like [[ScholarshipList]] where the ScholarshipList is different for each user we send an email to.

Ideally, at least in my mind, these email templates would be self contained .aspx pages. And would be invoked from our winform emailer application in a manner similar to

IEmailTemplate template = EmailTemplates.Load("ScholarshipList.aspx");
template.UserID=1234;
string emailMessage = template.Render();

Then the ScholarshipList.aspx page would look similar to any other asp.net page in that it can have server controls, runat="server" scripts and databind.

I've seen this article http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp which looks promising. Has anyone used it before? It uses some remoting which ideally would be avoided, and has a limited error handling mechanisms. I thought I'd get some other feedback before I went to far down that road.

+1  A: 

You could try this approach as well. http://jamesewelch.wordpress.com/2008/07/11/how-to-render-a-aspnet-user-control-within-a-web-service-and-return-the-generated-html/

If you hosted your templates in a webservice and then invoked them from your app.

David McEwing
A: 

A quick and dirty way to do it is to do a HttpWebRequest to the url and grab the output and use that, but thats not as nice as the anser from David...

pzycoman
A: 

You could also look at Smart Code, an opensource project for code Generation based on Asp.net templating.

http://www.codeplex.com/smartcodegenerator

It is integrated with Nant and Cassini so you can run it from the command line or from your WinForm app.

However for email, it is more common to use a simple String.Replace like you've mentioned above but if you need to do loops and the like then templates might be better.

To solve the [[ScholarshipList]] that you are mentioning without asp.net templates you can also use a text file like:

Hello [[MemberSimpleReplace][FIRSTNAME]],
Here is your ScholarShipList:
[[ScholarShip]]

And then use the factory method pattern to instantiate the correct factory (this is pseudo code, so bear with my mistakes)

IEmailContentGenerator gen = EmailContentGenFactory.getGenerator("MemberSimpleReplace");
var params = {"FIRSTNAME"};
var stringToSubstitute = gen.Generate(params,context);
var gen2 = EmailContentGenFactory.getGenerator("ScholarShip");
var params2 = {};
var stringToSubstitute2 = gen2.Generate(params2,context);

Then when you can easily implement new Generators, the generators get an array of parameters and the context that would have the UserId and maybe some other values.

Also remember that Asp.net is not the only template tool in .net to generate HTML, you can look at NVelocity for example or the many View Engines for Asp.net MVC.

Guillaume Gros