ASP.NET Write out contents of HTML file?

2019-08-28 21:44发布

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?

3条回答
Ridiculous、
2楼-- · 2019-08-28 21:47

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楼-- · 2019-08-28 21:51

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");
查看更多
手持菜刀,她持情操
4楼-- · 2019-08-28 21:52

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

        }
查看更多
登录 后发表回答