Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从html块中创建格式化字符串?_C#_Html_Winforms_Outlook_String Formatting - Fatal编程技术网

C# 如何从html块中创建格式化字符串?

C# 如何从html块中创建格式化字符串?,c#,html,winforms,outlook,string-formatting,C#,Html,Winforms,Outlook,String Formatting,我需要向字符串列表的(字符串)成员提供可变位数据(电子邮件的“正文”),并尝试使用string.format构建它,但我在此处的“http”部分得到“)expected”: string htmlBodyAmalgamation = string.Format(@"<html><body><img src=\"http://www.platypus.com/wp- content/themes/platypus/images/pa_logo_notag.png\"

我需要向字符串列表的(字符串)成员提供可变位数据(电子邮件的“正文”),并尝试使用string.format构建它,但我在此处的“http”部分得到“)expected”:

string htmlBodyAmalgamation = string.Format(@"<html><body><img src=\"http://www.platypus.com/wp-
content/themes/platypus/images/pa_logo_notag.png\" alt=\"Platypus logo\" width=\"199\" height=\"130\" ><p>{0}</p>", body);
string htmlbodyamalgation=string.Format(@“{0}

”,正文);
无论“http”前面是否有0、1、2或3个反击(\”),我都会收到错误消息

如果没有可变部分(如果主体是静态的/事先已知的),我可以这样做:

List<String> htmlBody = new List<string>
    {
        "<html><body><img src=\"http://www.platypus.com/wp-content/themes/platypus/images/pa_logo_notag.png\" alt=\"Platypus logo\" width=\"199\" height=\"130\" ><p>Your Platypus Price Push report is attached.</p>",
        "</body></html>"
    };
mailItem.HTMLBody = string.Join(Environment.NewLine, htmlBody.ToArray());
List htmlBody=新列表
{
“随函附上您的鸭嘴兽价格推送报告。

”, "" }; mailItem.HTMLBody=string.Join(Environment.NewLine,HTMLBody.ToArray());
…而且效果很好

因此,它试图通过string.format嵌入变量“body”值,如下所示,这被证明是有问题的:

string htmlBodyAmalgamation = string.Format(@"<html><body><img src=\"http://www.platypus.com/wp-content/themes/platypus/images/pa_logo_notag.png\" alt=\"Platypus logo\" width=\"199\" height=\"130\" ><p>Your Platypus Price Push report is attached.</p>", body);
List<String> htmlBody = new List<string>
    {
        htmlBodyAmalgamation,
        "</body></html>"
    };
string htmlBodyAmalgamation=string.Format(@“您的鸭嘴兽价格推送报告附在后。

”,正文); List htmlBody=新列表 { htmlBodyAmalgamation, "" };

让html和string.format一起工作的诀窍是什么?

我想与大家分享一个大约一年前使用的优雅解决方案。 我们在电子邮件中也使用了HTML模板,发现Razor引擎实际上(显然)非常擅长这样做

我们的代码如下:

    public string CompileMessage(string templateHtml, object modelClass)
    {
        try
        {
            TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
            {
                Resolver = new TemplateResolver(),
                Language = Language.CSharp,
                Debug = true,
            };

            Razor.SetTemplateService(new TemplateService(templateConfig));

            return Razor.Parse(templateHtml, modelCLass);
        }
        catch (Exception ex)
        {
            Logger.Log4Net.Error("Failed to compile email template using Razor", ex);
            return "Error occurred (" + ex.Message + "). Check the logfile in the vicinity of " + DateTime.Now.ToLongTimeString() + ".";
        }
    }
好处:

  • 使用Razor给了我们一个带有“占位符”的模型的类型安全性
  • 我们讨论了XSS和类似的攻击,因为Razor总是逃避值,除非使用
    @Html.Raw()
  • 它使业务用户能够相对安全地编辑模板,而不必担心与html不兼容的字符
  • Razor将负责格式化字符串中的区域性/ui区域性

似乎很简单-使用单引号而不是双引号,并省略反引号:

string htmlBodyAmalgamation = string.Format(@"<html><body><img src='http://www.platypus.com/wp-content/themes/platypus/images/pa_logo_notag.png' alt='Platypus logo' width='199' height='130' ><p>{0}</p>", body);
string htmlbodyamalgation=string.Format(@“{0}

”,正文);
更简单的是,我可以省去一些中间商,只需这样做:

mailItem.HTMLBody = string.Format(@"<html><body><img src='http://www.platypus.com/wp-content/themes/platypus/images/pa_logo_notag.png' alt='Platypus logo' width='199' height='130' ><p>{0}</p></body></html>", body);
mailItem.HTMLBody=string.Format(@“{0}

”,body);
这是一个Windows窗体应用程序。@B.ClayShannon您可以在MVC应用程序之外使用Razor,它实际上是RazorEngine库。另请参见上一个示例,您使用的是逐字操作符
@
并使用\进行转义。在这些字符串中,您实际上会逃脱<代码>“<代码> > <代码> <代码> > <代码> > />代码。使用单引号看起来更简单,似乎是有效的。对于简单的HTML内容,使用字符串格式是完全可以的,但是对于像电子邮件模板这样复杂的场景考虑使用。