ASP.NET Write out contents of HTML file?

I don’t know if this is a stupid question but..

Is it possible in either ASP.NET (either C# or VB#) to Response.Write() the contents of another HTML file? If so, how?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Read the HTML file line by line and write it using Response.Write()

 StreamReader sr = new StreamReader(@"C:\abc.html");
        while(sr.Peek() >= 0)
        {
           line=sr.ReadLine();
           Response.Write(line);

        }

Solution 2

You can get all the lines into a string array and send them out directly.

string[] lines = File.ReadAllLines("path/to/my/file.html");
foreach(string line in lines)
{
    Response.Write(line);
}

Just don’t forget to set your headers up correctly because this will just inject HTML. It won’t set up any special headers that might be expected (if any).

Solution 3

I know this is an old question, but I have another solution for future researching. How about just use TrasmitFile? i.e.:

Response.WriteFile(@"folder/filename.html");

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply