Render css when transforming view into string

Asked

Viewed 38 times

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 turn my view into string, to send by email, however, in the email the view runs out of 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 make css appear in the email ?

2 answers

1

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

0

As Gustavo Santos said the CSS files are not loaded in an email, the tag can be used <style> for Hotmail, Yahoo! , and Windows Live Mail, but it’s worth noting that in Gmail it takes the tag and its content.

I usually use a tool to inline CSS, the 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.