Include contents of an html page in an aspx page

If you are creating a new ASP.NET application, but have a huge collection of existing content in html files, one option is to move all the content into a database and generate pages dynamically. However, migration to a database can be a time-consuming task depending on the volume of content. So wouldn't it be easier to somehow import the relevant parts of the existing html pages into your aspx page?

The use of System.IO and regular expressions makes this a very easy task. Place a <asp:Literal> control (ID="htmlbody") on your page, and then use the following code to strip out everything up to and including the <body> tag (regardless of whether the tag contains additional attributes), and everything from the closing </body> tag onwards:

 StreamReader sr;
 string html;
 sr = File.OpenText("<path_to_file.htm>");
 html = sr.ReadToEnd();

 Regex start = new Regex(@"[\s\S]*<body[^<]*>", RegexOptions.IgnoreCase);
 html = start.Replace(html,"");
 Regex end = new Regex(@"</body[\s\S]*", RegexOptions.IgnoreCase);
 html = end.Replace(html, "");
 htmlbody.Text = html;

Date Posted:
Last Updated:
Posted by:
Total Views to date: 62281


- pranav

what does below code means?


- Mike


It's part of a regular expression pattern. It attempts to locate the body tag in the html, and allows for cases where there might be inline styling or javascript onload function calls. Or indeed anything else.

- Jason

I have a aspx page where I have it inside of a master page. I want to add javascript but I know you can only do this in a html page. I am just wanting to know is ther a way to add javascript to a aspx page.

Recent Comments

Kamil 13/10/2015 14:37
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Hello Mike, Any reason why the download link to the source code doesn't work anymore. I'd like I...

Niks Shine 13/10/2015 04:20
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
Hi, I am going to start learning MVC. Just for the guidance purpose, I would like to know...

biswajit 12/10/2015 09:54
In response to Solved - The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine
inspite of installing microsoft office acesss database engine 2007 as per your instruction on my of...

Don 11/10/2015 22:53
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
In addition to the typo in: public string FullName { get { return string.Format("{0] {1}", } you...

Steve 11/10/2015 17:32
In response to How To Send Email In ASP.NET MVC
Hi Mike, I found your article to be a paragon of clarity and described almost exactly my I am using...

satyabrata 11/10/2015 07:05
In response to Downloading multiple files in ASP.NET
Excellent article!!...

Edmond Kong 10/10/2015 22:06
In response to The WebGrid Helper - Making Selections
Hello Mike, The link to the download page does not work anymore. I'd like to download the sample...

Ervis 09/10/2015 15:10
In response to Creating a Connection String and Working with SQL Server LocalDB
Great tutorial. I do have a probem though, nothing is happening in my App_Data and I can't why....

Gavan McGregor 09/10/2015 13:45
In response to Downloading multiple files in ASP.NET
Hi, (long time reader, first time poster!) Although it's a deliberately simple example, It's /...

Rodrigo 08/10/2015 20:40
In response to MVC 5 with EF 6 in Visual Basic - Creating an Entity Framework Data Model