在string.Format转义用户输入的字符,如{0),{1}

时间:2015-05-13 21:43:26

标签: c# .net

发送SMTP电子邮件时出现以下错误:

  

索引(从零开始)必须大于或等于零且小于   参数列表的大小。

此错误是专门发生的,因为entry.RunDescription具有以下用户输入的文本,其中一些字符使电子邮件正文中的string.format方法混淆。

gradle/wrapper/gradle-wrapper.properties

由于这是一个用户文本,除了添加用户不输入这些字符的逻辑或者我如何逃避这些字符并仍然保留文本之外,我有什么方法可以使它工作吗?

distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zi

2 个答案:

答案 0 :(得分:3)

只需将字符串连接到格式外:

    message.Body = string.Format("<html><body>- Service Request<br/> A new service request is in queue <br/> Please use the following link to access the details of the Service Request <a href=\"{0}{1}\">{0}{1}</a> <br/>", 
   ServiceRequestURL, Entry.ServiceEntryID) +
   entry.RunDescription +
   "Thank you <br/> Administrator <br/></body></html>";

如果您仍想逃避,{应该使用{{进行回复,而}应该替换为}}

答案 1 :(得分:0)

请勿将用户输入的entry.RunDescription字符串“添加”为格式字符串。而是为它添加一个额外的参数:

message.Body = string.Format(" ... <br/>{2} ...",
    ServiceRequestURL, Entry.ServiceEntryID, entry.RunDescription));
相关问题