• In this article we will learn about the ASP.NET Core application to send customizable email templates using Razor pages, HTML & CSS.

     

    The RazorLite is a simplest way to create a template from string. Each template must have a templateKey that is associated with it, so you can render the same template next time without recompilation. The Razor Engine is fairly light weight and completely decoupled from the ASP.NET runtime and the HTTP processing. rather it is a pure template engine whose main purpose is to render text templates.

     

    We need to create a two projects:

    1. The main project that sends emails called WebAPI 

    2. The project for templates is called RazorLightTemplate

    Project Setup

     

    To start, we will create an ASP.NET Core 6.0 web Application with a project type of web API and call it WebAPI.

     

    Once the WebAPI project is created we will need to add a few directories to it to organize the code.

    • Create a controller called EmailCotroller.cs inside the Controllers folder.

    • Create a directory called Templates.

    • Inside Templates, create file EmailTemplate.cshtml

    Next, we need  to create another project called RazorLightTemplate by going to a new Asp.net Core 6.0  web application and selecting the class library in the project type. 

     

    Once the RazorLightTemplate project is created we will want to add a few directories to it to organize the code.

    • Create a directory called Contracts.

    • Inside Contracts create a directory called TemplateManagement.

    • Inside Views create a directory called Model.

    • Inside Emails create a class called EmailModel.cs

     

    Now, Install the nuget package using following command in RazorLightTemplate project:

     

    Install-Package RazorLight.NetCore3 -Version 3.0.2

     

    You can also install it from nuget package manager.

    Now let’s add some code to implement RazorLight.

     

    ITemplateExecutionEngine.cs

    RazorLightTemplateExecutionEngine.cs

     

     It will use the Razor engine to find the view we specify and build a rendered version of it to be returned as a string. RazorLight resolves templates from any source, The built-in providers that resolve template source from filesystem and embedded resources. Then root namespace and template name will be combined into WebAPI.Templates.EmailTemplate.cshtml. This code renders a template from a string input and returns a result back as a string.

     

    To retrieve a compiled template:

    ITextTemplate.cs

    TextTemplate.cs

     

    Next for the project setup we need to add a reference to the RazorLightTemplate project in our WebAPI project.

     

    Sending the Email

    The final step is to send the email. We will create a simple endpoint that will call the EmailModel class to generate the email content.

    EmailModel.cs

    In the Api project under EmailController.cs and add a simple GET method to send an email.

     

     

    EmailTemplate.cshtml

    In the Program.cs we need to add the following line to add services.

     

    Final step is to build the project, and press F5. and goto https://localhost:7136/swagger/index.html 

    Output:

    Note: If you are getting "Cannot find compilation library" when run this project. Add these property groups to your entry point csproj.

     

    Thank you for reading…

0 Years in
Operation
0 Loyal
Clients
0 Successful
Projects

Words from our clients

 

Tell Us About Your Project

We’ve done lot’s of work, Let’s Check some from here