How to include CSS styles inline in Razor view?

后端 未结 3 1006
清歌不尽
清歌不尽 2020-12-28 16:23

I am using Postal to render MVC Razor views and send them via email. I have a custom CSS that I have defined specifically for the email views. Currently I am including them

3条回答
  •  没有蜡笔的小新
    2020-12-28 16:52

    Upvoted Paul d'Aoust's answer, but I found this version of his helper method to work a little better for me (wouldn't try to encode things like quotes in the CSS):

    public static class CssHelper
    {
      public static IHtmlString EmbedCss(this HtmlHelper htmlHelper, string path)
      {
        // take a path that starts with "~" and map it to the filesystem.
        var cssFilePath = HttpContext.Current.Server.MapPath(path);
        // load the contents of that file
        try
        {
          var cssText = File.ReadAllText(cssFilePath);
          return htmlHelper.Raw("");
        }
        catch
        {
          // return nothing if we can't read the file for any reason
          return null;
        }
      }
    }
    

提交回复
热议问题