ASP.NET Write out contents of HTML file?

2019-08-28 21:39发布

问题:

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?

回答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);

        }


回答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).



回答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");