How to include an HTML document in a .NET application with variable fields?

I have a C # /. NET application that currently uses StringBuilder to generate HTML email messages.

A new message format has been developed and now includes more formatting and CSS. I would like to avoid adding each line of the file using StringBuilder, so I thought it was best to include the HTML file as a resource.

However, there are about 21 variables in CSS and HTML that I need to change on the fly. My first thought was to replace them with standard String.Format placeholders ({0}, {1}, etc.), but when viewing the HTML, the checks confirm this.

I'm pretty dumb about what works best for storing a 200-line HTML file and changing parts of it before being included in an email.

Example:

In CSS, I need to change the color of some elements, for example:

#header
{
    background-color: {0};
}

Inside HTML, I need to change strings and URLs, for example:

<img src="{1}" />
<span>{2}</span>

It seems that HTML as a resource in the project will be better, but trying to use String.Format with this resource, regardless of whether it will work, is a weak method due to the above validation errors.

Any suggestions?

+5
source share
6 answers

{1} , . Table1HeaderImage, String.Format String.Replace. , thehtml, , ..

+2

, t4 tamplate

t4 ,

<table>
<# for (int i = 1; i <= 10; i++)
   { #>
     <tr><td>Test name <#= i #> </td>
         <td>Test value <#= i * i #> </td> </tr>
 <# } #>
</table>
+4

Razor HTML:

<h2>Items:</h2>
@foreach(var item in list)
{
    <p>Item: @item.Description</p>
}

Razor View Engine asp.net, .

+2

, :

StreamReader reader = new StreamReader(Server.MapPath("Email/email.html"));
string email_html = reader.ReadToEnd();
reader.Close();

email_html = email_html.Replace("[link]", activation_link);
0

<%0%>, <%1%> .. {0}, {1} ..

:

  • { {{ } }}
  • <% { %> }
  • String.Format()

, , . :

  • <%name%> dictionary["name"]
0

All Articles