HTML 帮助器:
1. Html.Raw 方法:
- Html.Raw(content) 用于在 Razor 页面中插入未编码的 HTML 内容。
- 示例:@Html.Raw("<strong>Hello, World!</strong>")
2. Html.Encode 方法:
- Html.Encode(content) 用于对 HTML 内容进行 HTML 实体编码。
- 示例:<p>@Html.Encode("This is some text.")</p>
3. Html.ActionLink 方法:
- Html.ActionLink(linkText, action, controller, routeValues, htmlAttributes) 用于生成一个超链接。
- 示例:@Html.ActionLink("Home", "Index", "Home")
表单帮助器:
1. Html.BeginForm 和 Html.EndForm 方法:
- Html.BeginForm(action, controller, method, htmlAttributes) 用于生成 HTML 表单的开始标签。
- Html.EndForm() 用于生成 HTML 表单的结束标签。
- 示例:@using (Html.BeginForm("Login", "Account", FormMethod.Post)) { /* form content */ }
2. Html.TextBox 和 Html.Password 方法:
- Html.TextBox(name, value, htmlAttributes) 用于生成文本框。
- Html.Password(name, value, htmlAttributes) 用于生成密码框。
- 示例:@Html.TextBox("UserName")
图像帮助器:
1. Html.Image 方法:
- Html.Image(src, alt, htmlAttributes) 用于生成图像标签。
- 示例:@Html.Image("~/Content/Images/logo.png", "Logo")
URL 帮助器:
1. Href 方法:
- Href("~/path/to/page") 用于生成相对于应用程序根目录的 URL。
- 示例:<a href="@Href("~/Home/Index")">Home</a>
备注:
- ASP.NET Web Pages 还提供了一些其他的 HTML、表单和 URL 相关的帮助器,用于处理各种任务,如列表生成、表格生成、分页等。
使用示例:
@{
var message = "Hello, World!";
}
<!DOCTYPE html>
<html>
<head>
<title>Helper Example</title>
</head>
<body>
<h1>@Html.Encode(message)</h1>
@using (Html.BeginForm("Submit", "Home", FormMethod.Post))
{
<p>@Html.TextBox("inputText")</p>
<p><input type="submit" value="Submit" /></p>
}
</body>
</html>
以上只是 ASP.NET Web Pages 中一些常见帮助器的基本用法。对于更详细和全面的信息,请查阅[官方文档](https://learn.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-razor-syntax)。在文档中,你将找到有关各种帮助器和 Razor 语法的详细信息。
转载请注明出处:http://www.pingtaimeng.com/article/detail/6547/ASP.NET