Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to generate an HTML file?

I'm working on a console application that's supposed to spit out an html document that contains a table and maybe some javascript.

I thought about writing the html by hand:

streamWriter.WriteLine("<html>");
streamWriter.WriteLine("<body>");
streamWriter.WriteLine(GetHtmlTable());
streamWriter.WriteLine("</body>");
streamWriter.WriteLine("</html>");

... but was wondering if there is a more elegant way to do it. Something along these lines:

Page page = new Page();
GridView gridView = new GridView();
gridView.DataSource = GetDataTable();
gridView.DataBind();

page.Controls.Add(gridView);
page.RenderControl(htmlWriter);
htmlWriter.Flush();

Assuming that I'm on the right track, what's the proper way to build the rest of the html document (ie: html, head, title, body elements) using the System.Web.UI.Page class? Do I need to use literal controls?

like image 979
John Smith Avatar asked Dec 09 '25 20:12

John Smith


1 Answers

It would be a good idea for you to use a templating system to decouple your presentation and business logic.

Take a look at Razor Generator which allows the use of CSHTML templates within non ASP.NET applications.

http://razorgenerator.codeplex.com/

like image 114
Craig Bovis Avatar answered Dec 12 '25 08:12

Craig Bovis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!