C#返回HTML字符串的方法格式错误,寻求排查解决
解决C#逐字字符串中动态URL拼接的语法错误
错误原因分析
你修改代码时的核心问题有三个:
- 未正确闭合逐字字符串就直接拼接变量,导致语法逻辑混乱
- 路径片段
/AuditProject/OngoingAudits未被当作字符串字面量(缺少引号包裹),编译器会将其识别为未知符号 - HTML的href属性引号未正确转义或闭合
修正方案
推荐两种简洁且不易出错的写法:
方案1:使用C#字符串插值(C# 6+支持)
逐字字符串支持插值,只需在@后添加$,用{变量名}直接嵌入动态内容,同时保持HTML引号的转义(用""表示单个双引号):
private static string LoadRemovedSheetTemplate(string url) { return $@"<!DOCTYPE html> <html> <head> <title></title> <meta charset=utf-8 /> </head> <body style=""margin: 0; padding: 10px; box-sizing: border-box;background-color: #f9f9fa;margin: 20px;""> <div class=""container"" style=""margin: 0; padding: 0; box-sizing: border-box;background-color: #f9f9fa; margin-left: auto; margin-right: auto; width: max-content;""> <div class=""main-content-section"" style=""background-color: #fff;height: 600px;margin-bottom: 20px; box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px;padding: 25px; height: max-content; width: max-content;""> <div class=""top-logo-section"" style=""text-align: center;""> <div style=""height: 100px; margin-bottom: 10px;""> <img src=""https://dwihn.org//images/logo.png"" alt=""BizAnalytix Technologies"" style=""height: 100%;"" /> </div> </div> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;"">Dear {2},</p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;""> Claim has been removed from the audit {3}. </p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;""> <a href=""{url}/AuditProject/OngoingAudits"">Click here</a> to view. </p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;"">Thank You,</p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;"">CATS Support</p> <div class=""bottom-logo-section"" style=""text-align: center;""> <div style=""margin:0;""> <p style=""font-size: 14px; font-family: sans-serif; color: #000000;""> Powered By: </p> <img src=""https://bizanalytix.com/wp-content/uploads/2020/12/BizAnalytix-New-Logo.png"" style=""height:50px;"" /> </div> </div> </div> </div> </body> </html>"; }
方案2:正确的字符串拼接
若必须使用拼接方式,需确保逐字字符串、变量、字符串片段都正确闭合和连接:
private static string LoadRemovedSheetTemplate(string url) { return @"<!DOCTYPE html> <html> <head> <title></title> <meta charset=utf-8 /> </head> <body style=""margin: 0; padding: 10px; box-sizing: border-box;background-color: #f9f9fa;margin: 20px;""> <div class=""container"" style=""margin: 0; padding: 0; box-sizing: border-box;background-color: #f9f9fa; margin-left: auto; margin-right: auto; width: max-content;""> <div class=""main-content-section"" style=""background-color: #fff;height: 600px;margin-bottom: 20px; box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px;padding: 25px; height: max-content; width: max-content;""> <div class=""top-logo-section"" style=""text-align: center;""> <div style=""height: 100px; margin-bottom: 10px;""> <img src=""https://dwihn.org//images/logo.png"" alt=""BizAnalytix Technologies"" style=""height: 100%;"" /> </div> </div> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;"">Dear {2},</p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;""> Claim has been removed from the audit {3}. </p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;""> <a href=""" + url + @"/AuditProject/OngoingAudits"">Click here</a> to view. </p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;"">Thank You,</p> <p style=""font-size: 14px; font-family: sans-serif; line-height: 25px; color: #000000; letter-spacing: 0.2px;"">CATS Support</p> <div class=""bottom-logo-section"" style=""text-align: center;""> <div style=""margin:0;""> <p style=""font-size: 14px; font-family: sans-serif; color: #000000;""> Powered By: </p> <img src=""https://bizanalytix.com/wp-content/uploads/2020/12/BizAnalytix-New-Logo.png"" style=""height:50px;"" /> </div> </div> </div> </div> </body> </html>"; }
关键说明
- 字符串插值(
$@"")是最简洁的方式,直接嵌入变量无需手动拼接,大幅降低语法错误概率 - 拼接时需注意:逐字字符串结束后用
""转义HTML双引号,连接变量后再用@""包裹路径片段,确保斜杠无需转义 - 最终生成的HTML需保证href属性引号完整,输出格式应为
<a href="传入的URL/AuditProject/OngoingAudits">
内容的提问来源于stack exchange,提问作者heartbeat
相关产品推荐
相关产品推荐

