如何设置 C# 项目以从本地 .css 文件将 css 内联到 HTML 中? PreMailer.Net可以做到吗?
How do I set up a C# project to inline css into HTML from a local .css file? Can PreMailer.Net do this?
我正在尝试更新电子邮件模板系统以允许使用外部 .css 文件,而不必内联编写所有样式。我的项目在 ASP.NET 中,据我所知,最常用且功能丰富的 inliner 是 PreMailer.Net,但如果有更适合我的包,我愿意使用不同的包需要。这是我正在尝试做的事情的简要说明:
文件系统
G:/templates/
index.html
style.css
inliner.cs
string htmlSource = File.ReadAllText("G:\templates\index.html");
var uri = new Uri("G:\templates\");
var result = PreMailer.MoveCssInline(uri, htmlSource);
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href='style.css'>
</head>
<body>
</body>
</html>
当我 运行 此代码时 - 我收到以下错误:
Unable to cast object of type 'System.Net.FileWebResponse' to type 'System.Net.HttpWebResponse'.
根据文档,我不清楚 PreMailer.Net 是否支持此用例。它声称如果指定 BaseUri 参数,则可以使用相对 URL。 C# 代码 运行ning PreMailer.Net 具有必要的上下文和对该文件路径的访问权限,事实证明它可以将 HTML 文件读入字符串。
这个问题似乎已在刚刚发布的 2.1.1 as per this GitHub 问题中得到解决:
Add support on fetching Uri with local filepath like
"file:///C:/website/style.css"
It's required when we optimize the performance by fetching the local
resource internally instead of making external web request towards
internal resource. e.g. As I know the css is actually inside the local
storage, it make sense to just make a fast local fetch
("file:///C:/website/style.css") instead of external web request
("https://www.example.com/style.css")
So I can now initialize a PreMailer instance with BaseUri "file:///" +
new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~/"),
UriKind.Absolute)
It's how I boost the performance for my client, just share my code
here.
我正在尝试更新电子邮件模板系统以允许使用外部 .css 文件,而不必内联编写所有样式。我的项目在 ASP.NET 中,据我所知,最常用且功能丰富的 inliner 是 PreMailer.Net,但如果有更适合我的包,我愿意使用不同的包需要。这是我正在尝试做的事情的简要说明:
文件系统
G:/templates/
index.html
style.css
inliner.cs
string htmlSource = File.ReadAllText("G:\templates\index.html");
var uri = new Uri("G:\templates\");
var result = PreMailer.MoveCssInline(uri, htmlSource);
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href='style.css'>
</head>
<body>
</body>
</html>
当我 运行 此代码时 - 我收到以下错误:
Unable to cast object of type 'System.Net.FileWebResponse' to type 'System.Net.HttpWebResponse'.
根据文档,我不清楚 PreMailer.Net 是否支持此用例。它声称如果指定 BaseUri 参数,则可以使用相对 URL。 C# 代码 运行ning PreMailer.Net 具有必要的上下文和对该文件路径的访问权限,事实证明它可以将 HTML 文件读入字符串。
这个问题似乎已在刚刚发布的 2.1.1 as per this GitHub 问题中得到解决:
Add support on fetching Uri with local filepath like "file:///C:/website/style.css"
It's required when we optimize the performance by fetching the local resource internally instead of making external web request towards internal resource. e.g. As I know the css is actually inside the local storage, it make sense to just make a fast local fetch ("file:///C:/website/style.css") instead of external web request ("https://www.example.com/style.css")
So I can now initialize a PreMailer instance with BaseUri "file:///" + new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~/"), UriKind.Absolute)
It's how I boost the performance for my client, just share my code here.