Search code examples
c#csslessrazorengine

How to use css/less styles in RazorEngine template


I'd like to generate a great amount of different mails by the use of Antaris RazorEngine. Is there a possibility to include script and style bundles to the layout? We build those bundles dynamically together, so they include many different .css/.js-files.

In our non-e-mail layouts we always include them like that:

@Styles.Render("~/bundles/" + theme + "/styles") 
@Scripts.Render("~/bundles/scripts") 
@RenderSection("scripts", false)

I don't want to use inline-styling, as this would stretch each view extremely and would be hard to maintain.

I tried to google this issue, but I haven't found, what I was searching for.

Could anyone explain, how to do this and if it doesn't work like I actually need it, explain why and how to do it in an other way?


Solution

  • Well, since no one could answer it, I used Premailer to move my .less files (or the .css generated out of it) inline. Works for me, even it's not exactly what I wanted.

    string viewString = System.IO.File.ReadAllText(viewPath); // view to string
    string cssSiteString = System.IO.File.ReadAllText(cssSitePath); // css-file to string
    
    Engine.Razor.AddTemplate(nameoftemplate, viewString);
    Engine.Razor.Compile(viewPath);
    
    var result = Engine.Razor.Run(viewPath, null, model, viewBag);
    
    var pm = new PreMailer.Net.PreMailer(result); 
    var completeMail = pm.MoveCssInline(css: cssSiteString); // this line moves the css inline