Write HTML to string

前端 未结 18 1475
一生所求
一生所求 2020-12-13 00:04

I have code like this. Is there a way to make it easier to write and maintain? Using C# .NET 3.5.

string header(string title)
{
    StringWriter s = new Stri         


        
18条回答
  •  独厮守ぢ
    2020-12-13 00:18

    With the introduction of Razor in ASP.net MVC, the best way to write HTML in C# is with the Razor Engine.

    string templatePath = $@"{Directory.GetCurrentDirectory()}\EmailTemplates"; 
    
    IRazorLightEngine engine = EngineFactory.CreatePhysical(templatePath); 
    
    var model = new Notification 
    { 
           Name = "Jone", 
           Title = "Test Email", 
           Content = "This is a test" 
    }; 
    
    string result = engine.Parse("template.cshtml", model); 
    

    Template:

    Dear @Model.Name, you have a notification.

    @Model.Title

    @Model.Content

    Date:@DateTime.Now

    For a complete sample, see here

提交回复
热议问题