1: I have an .html file with some markup with some placeholder tags.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> First Name : <FIRSTNAME/> <br /> Last Name: <LASTNAME/> </body> </html>
2: I have a class for storing data returned from db
public class Person { public Person() { } public string FirstName { get; set; } public string LastName { get; set; } }
3: PersonInfo.aspx I write this .html with a replaceable placeholder with actual values.
public partial class PersonInfo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Person per= new Person(); per.FirstName = "Hello"; per.LastName = "World"; string temp = File.ReadAllText(Server.MapPath("~/template.htm")); temp = temp.Replace("<FIRSTNAME/>", per.FirstName); temp = temp.Replace("<LASTNAME/>", per.LastName); Response.Write(temp); } }
4: PersonInfo.aspx is literally empty since I am inserting html from the code.
When PersonInfo.aspx is called, the layout of the html template will be displayed with the corresponding values ββin the placeholder. It is also likely that I want to send the final markup to the html email address (although this is not part of the question since I know how to send email).
Is this the best way to populate the values ββin my html template or some other better alternative?
Note. This is a very simple example. My class is very complex with objects as properties, and also my html template contains 40-50 placeholders.
So the code in my step 3 will require 40-50 Replace statements.
Feel free to ask if there are any doubts and whether you really appreciate any inputs.
source share