将动态内容插入静态HTML

时间:2020-05-13 06:54:38

标签: c# html .net asp.net-mvc

我的要求是将一些动态内容从数据库插入到静态HTML文件中。我可以使用javascript在客户端执行此操作。

实际要求是从数据库中获取值并将这些值插入静态HTML的字段中。

例如,这就是我认为静态HTML的外观

<html>
   <head>
   </head>
   <body>
      <div>
         <p>The first name is (Insert first name from DB)</p>
         <p>The second name is (Insert last name from the DB)</p>
      </div>
   </body>
</html>

静态HTML已在本地保存,或者可能已存在于服务器上。

我正计划在.NET中构建一个控制台应用程序来执行此操作。我将如何实现这一目标?

我已经搜索了该网站,但没有看到类似的内容。

1 个答案:

答案 0 :(得分:1)

现在我明白了。

假设这样的模板:

<html>
    <head> </head>
    <body>
        hi ##YourName##
    </body>
</html>

您的程序可能如下:

class Program
{
    static void Main(string[] args)
    {
        // load template
        var template = System.IO.File.ReadAllText(@"c:\temp\template.html");

        var content = template.Replace("##YourName##", "value From Db");

        System.IO.File.WriteAllText(@"C:\temp\static_html_file_1.html", content);
    }
}

结果为:

<html>
    <head> </head>
    <body>
        hi value From Db
    </body>
</html>