Search code examples
c#asp.net-mvcemailtemplatesspark-view-engine

ASP.NET MVC Email


Is their a solution to generate an email template using an ASP.NET MVC View without having to jump through hoops.

Let me elaborate jumping through hoops.

var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
var oldContext = HttpContext.Current;
HttpContext.Current = fakeContext;
var html = new HtmlHelper(new ViewContext(fakeControllerContext,
  new FakeView(), viewDataDictionary, new TempDataDictionary()),
  new ViewPage());
html.RenderPartial(viewName, viewData, viewDataDictionary);
HttpContext.Current = oldContext;

The above code is using the current HttpContext to fake a new Context and render the page with RenderPartial, we shouldn't have to do this.

Another very detailed solution using ControllerContext and .Render: (IEmailTemplateService, Headers/Postback WorkAround) but pretty much doing the same thing with a lot more code.

I on the other hand, am looking for something that would just render a View without having to POST/GET and generates me a simple string that I can send off through my Email code. Something that doesn't run into errors such as posting headers twice or faking some piece of data.

EX:

//code which does not fire Render, RenderPartial... etc
var email = emailFramework.Create(viewData, view); 

See my solution bellow or follow this link:

My Solution using spark: (12/30/2009) ASP.NET MVC Email Template Solution


Solution

  • This is what I wanted the ASP.NET MVC ViewEngine to do, but it's in Spark, just follow the latest link right bellow,

    Update (12/30/2009) Cleaner Version: ASP.NET MVC Email Template Solution


    (11/16/2009) Or, Louis DeJardin Console Application Version:

    using System;
    using Spark;
    using Spark.FileSystem;
    
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public abstract class EmailView : AbstractSparkView
    {
        public User user { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            // following are one-time steps
    
            // create engine
            var settings = new SparkSettings()
                .SetPageBaseType(typeof(EmailView));
    
            var templates = new InMemoryViewFolder();
            var engine = new SparkViewEngine(settings)
                         {
                             ViewFolder = templates
                         };
    
            // add templates
            templates.Add("sample.spark", @"Dear ${user.Name}, This is an email.Sincerely, Spark View Engine http://constanto.org/unsubscribe/${user.Id}");
    
            // following are per-render steps
    
            // render template
            var descriptor = new SparkViewDescriptor()
                .AddTemplate("sample.spark");
    
            var view = (EmailView)engine.CreateInstance(descriptor);
            view.user = new User { Id = 655321, Name = "Alex" };
            view.RenderView(Console.Out);
            Console.ReadLine();
        }
    }
    

    I decided to use this method because it seems to be the one that does everything right, it:

    • Does not use any HttpContext/ControllerContext or mess with routing data!
    • It can implement Header/Footer to allow templates!
    • You can use loops, conditionals, etc...
    • It's clean, light weight especially if you plan to move entirely to spark view engine!

    Please, make sure to read these posts. All credit to Louis DeJardin see his tutorials :): Using Spark as a general purpose template engine!, Email Templates Revisited