Include contents of an html page in an aspx page

4.22 (18 votes)

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();
 sr.Close();

 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: 63876

5 Comments

- pranav

what does below code means?

"[\s\S]*<body[^<]*>"

- Mike

@pranav

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.

- khalid

You have a style class created in MyStyles.css style sheet document. What do you need to include in the HTML code on .aspx page to make sure that you can use the styles from MyStyles on that page?

- Mike

@Khalid,

You can use a link element to reference the style sheet in the aspx file.

Recent Comments

Praveen 12/02/2016 14:22
In response to Migrating Classic ASP To ASP.NET Razor Web Pages Part One- Razor Syntax And Visual Basic
Nicely written article, just what I wanted to get me started, I am going to start working on a this...

Whitney W. 11/02/2016 15:37
In response to Adding A Controller
I am really new to everything and just started programming. I really need help in my project since I...

Fredrik 11/02/2016 13:10
In response to Request.Form Is Empty When Posting To ASPX Page
It worked. Thank you!...

David Valdez 11/02/2016 03:08
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Muchas, muchas, gracias. Thank you so much from Dominican Republic....

Zahid 10/02/2016 00:42
In response to How To Send Email In ASP.NET MVC
Hello Sir, Great post. Just a quick question, is it possible if we can ask a client to save in an...

Al Wilton 07/02/2016 03:11
In response to Windows Authentication With ASP.NET Web Pages
I've been using this advise for quite a while. Today I was setting up a .NET 4.6.1 site and it drove...

Anders 06/02/2016 15:38
In response to iTextSharp - Working with Fonts
Thanks a lot for this excellent series on iTextSharp....

J_R 06/02/2016 02:45
In response to Simple File Download Protection with ASP.NET
Mike, Thank you for taking the time to write this. It really helped me - though I could not out a...

Nemat 04/02/2016 16:24
In response to Solved - The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine
Installing Microsoft Access Database Engine 2010 64 bit helped me. Thanks A lot!...

Bill Barbour 03/02/2016 18:34
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Wonderful example. I have it all working. I would like to add the image to each row of the index you...