在ASP.NET Razor中,你可以直接在.cshtml文件中编写HTML代码,同时嵌入C#代码以实现动态生成内容。以下是一个简单的例子,演示如何在ASP.NET Razor中编写HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>ASP.NET Razor HTML Example</title>
</head>
<body>

    <h1>Welcome to My Website</h1>

    <p>This is a paragraph of static HTML content.</p>

    @{
        // 动态生成当前日期
        var currentDate = DateTime.Now.ToString("yyyy-MM-dd");
    }

    <p>Current Date: @currentDate</p>

    <ul>
        @for (int i = 1; i <= 5; i++)
        {
            <li>Item @i</li>
        }
    </ul>

</body>
</html>

在这个例子中:

  •  <h1>和<p>标签表示HTML中的静态内容。

  •  @{ ... } 中的代码块用于在页面中生成当前日期。

  •  @for (int i = 1; i <= 5; i++) 是一个C#循环,用于动态生成5个列表项。


总体来说,ASP.NET Razor允许你直接混合HTML和C#代码,使得在页面中动态生成内容变得非常方便。




转载请注明出处:http://www.pingtaimeng.com/article/detail/14890/ASP.NET Razor 标记