I have a c# method that returns a string literal containing HTML markup. Initially, I hard-coded a URL, but now I want to generate a dynamic URL. However, I am encountering errors, likely due to incorrect syntax, but I am having trouble pinpointing the exact issue.
Can someone help me identify where I’m going wrong or what’s causing the problem?
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=""localhost:54400/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>";
}
In here <a href=""localhost:54400/AuditProject/OngoingAudits"">
I want to replace the localhost with the url which I am getting in my function
I tried this approach, <a href=" + url + /AuditProject/OngoingAudits">
, but it’s giving a lot of errors.
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
Answers
In your second approach, you’re trying to concatenate the url variable directly into the HTML string. However, the syntax is incorrect because you’re not properly enclosing the concatenated string within double quotes.
Try raw string literals with string interpolation. This makes it all a bit easier on the eyes.
But since you’re already using
string.Format()
placeholders in your HTML, you should make sure to escape those. This can be done by instructing the string interpolation to expect double curly braces with$$
:Note how you don’t need double double quotes
""
in your string anymore. The resulting HTML will also be indented properly.The variable
html
will contain: