Rendering css when transforming view into string

0

I'm using the method:

protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }

To transform my view into a string, to send by email, however, in the email the view is without css, even adding the css in <head>

<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/bootstrap-custom.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />

How do I get the e-mail to appear in the css?

    
asked by anonymous 09.07.2018 / 15:06

2 answers

2

CSS files are not loaded into emails, the way to style an email sent with HTML is inline style, or to load a style tag (in the same email string) with all classes and then use the classes.

    
09.07.2018 / 16:03
1

As Gustavo Santos said CSS files are not loaded in an email, the <style> tag can be used for Hotmail, Yahoo !, and Windows Live Mail, but it is worth mentioning that in GMail he takes the tag and his content.

I usually use a tool to inline CSS, PreMailer :

string html = /*busca o seu html*/;

using(var preMailer = new PreMailer(html))
{
     //Aqui podem ser passados parâmetros, como BaseUri, para montar os <link>'s corretamente
     html = pm.MoveCssInline();
}
    
10.07.2018 / 15:11