在 ASP.NET webform 中动态输出 html code/snippets

Dynamically outputting html code/snippets inside ASP.NET webform

我正在寻找一种方法,如果可能的话,遍历包含多个 HTML 文件(片段)的子文件夹,然后在一个 ASP.NET 应用程序中输出这些文件的内容页。

因此,如果将新片段添加到子文件夹,应用程序将自动更新,输出其内容。

我想你需要这个>

ASPX:

<asp:Literal runat="server" ID="lthtml"></asp:Literal>

CS:

 protected void Page_Load(object sender, EventArgs e)
    {
        string[] files = Directory.GetFiles("directory", "*.html");
        StringBuilder htmlContent = new StringBuilder();
        foreach (string f in files)
        {

            htmlContent.Append(ReadHtmlFile(f));
        }

        lthtml.Text = htmlContent.ToString();
    }


   public static StringBuilder ReadHtmlFile(string htmlFileNameWithPath)
   {
            System.Text.StringBuilder htmlContent = new    System.Text.StringBuilder();
            string line;
            try
            {
                using (System.IO.StreamReader htmlReader = new System.IO.StreamReader(htmlFileNameWithPath))
                {

                    while ((line = htmlReader.ReadLine()) != null)
                    {
                        htmlContent.Append(line);
                    }
                }
            }
            catch (Exception objError)
            {
                throw objError;
            }

            return htmlContent;
        }