In current web applications, sending email from template with dynamic content is a common requirement. To achieve this we add a template and we add some parameters which need to be replaced dynamically in the template. Let’s say we have user reset password email template like:
Hello ,
Your username:
Your new, reset password is
The Email Address you provided for your account is
So to send user specific email we do string replace for , , with actual values. This can be done in a easier manner by using razor parsing.
Steps to implement razor parsing:
1) Install Razor engine 2) Create Model 3) Change template to use model
4) Parse the template content using model
1) Install Razor engine ======================
Razor engine in avaliable in Nuget package:
To install from Nuget console use command: Install-Package RazorEngine
2) Create Model ================== Create a class with the template place holders as properties of the class.
For user reset password email template, sample model will be:
public class UserModel { public string UserName{get;set;} public string Password{get;set;} public string EmailAddress{get;set;} }
3) Change template to use model ============================== Now it is the tricky part! We need to change the template so that we can use the model properties easily.
For the reset password email, the modified template is:
Hello @Model.UserName,
Your username: @Model.UserName
Your new, reset password is @Model.Password
The Email Address you provided for your account is @Model.EmailAddress
4) Parse the template content using model ========================================
This is the last step. We just need to parse the template using model. To do so we need to use Razor.Parse() method.
public static string ParseTemplate(T model, string content) { return Razor.Parse(content, model); }
We need to pass the model and the template to get the razor parsed output. Here ParseTemplate() accepts any type of model and template content as string.
Example:
public void GetParsedContent() { //Initialize the model property values var userModel = new UserModel { UserName = "testuser1", Password = "password123", EmailAddress = "[email protected]" }; //Get the template content from file system (optional) var templatePath= HttpContext.Current.Server.MapPath("template.cshtml"); var templateContent = string.Empty; using (var reader = new StreamReader(templatePath)) { templateContent = reader.ReadToEnd(); } //Just call ParseTemplate() method var result = ParseTemplate(userModel, templateContent); }
You can get the template content from database, from file system or directly use custom hardcoded string. If you want to parse a complete file using razor engine name the file with “.cshtml” as extension.
More Read: ==========
1) http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax
2) http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
3) http://blog.mirajavora.com/render-email-templates-using-razor-engine