🛠️ 类的定义与使用

ASP.NET Web Pages 允许你使用 C# 或 VB 定义类,并在页面中实例化和使用它们。类是一种面向对象的编程结构,可以帮助你封装逻辑、重用代码并提高代码的可维护性。

🔑 基本类的定义

在 ASP.NET Web Pages 中,类通常定义在独立的 .cs 或 .vb 文件中,或直接嵌入在 .cshtml 或 .vbhtml 页面中。以下展示了在不同位置定义和使用类的例子。


🧳 C# 类定义与使用

📄 示例 1:在 PageModel.cs 中定义一个 C# 类

  1. 在项目的根目录或 App_Code 文件夹中创建一个新的类文件 PageModel.cs
public class PageModel
{
    public string Title { get; set; }
    public string Content { get; set; }

    public PageModel(string title, string content)
    {
        Title = title;
        Content = content;
    }

    public string GetFormattedContent()
    {
        return $"<h2>{Title}</h2><p>{Content}</p>";
    }
}

  1. 在 .cshtml 页面中使用该类:
@{
    var pageModel = new PageModel("欢迎", "这是我的第一个 ASP.NET Web Page!");
    var formattedContent = pageModel.GetFormattedContent();
}
<div>
    @Html.Raw(formattedContent)
</div>

在这个例子中,PageModel 类定义了 Title 和 Content 属性以及一个方法 GetFormattedContent,它返回格式化后的 HTML 内容。然后在 Razor 页面中,我们实例化这个类并调用其方法。


🧳 VB 类定义与使用

📄 示例 1:在 PageModel.vb 中定义一个 VB 类

  1. 在项目的根目录或 App_Code 文件夹中创建一个新的类文件 PageModel.vb
Public Class PageModel
    Public Property Title As String
    Public Property Content As String

    Public Sub New(title As String, content As String)
        Me.Title = title
        Me.Content = content
    End Sub

    Public Function GetFormattedContent() As String
        Return "<h2>" & Title & "</h2><p>" & Content & "</p>"
    End Function
End Class

  1. 在 .vbhtml 页面中使用该类:
@Code
    Dim pageModel = New PageModel("欢迎", "这是我的第一个 ASP.NET Web Page!")
    Dim formattedContent = pageModel.GetFormattedContent()
End Code

<div>
    @Html.Raw(formattedContent)
</div>

与 C# 示例类似,PageModel 类在 VB 中定义了 Title 和 Content 属性,以及 GetFormattedContent 方法。在 Razor 页面中实例化类并调用方法。


⚡ 使用类来增强页面逻辑

📄 示例 2:使用类处理表单数据

在实际开发中,类经常用来处理表单数据、进行数据验证或与数据库交互。

  1. 创建一个 UserModel.cs 类来处理用户信息:
public class UserModel
{
    public string Name { get; set; }
    public string Email { get; set; }

    public bool IsValid()
    {
        return !string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Email);
    }
}

  1. 在 .cshtml 页面中创建表单并处理数据:
@{
    var message = string.Empty;
    if (Request.Form["submit"] != null)
    {
        var user = new UserModel
        {
            Name = Request.Form["name"],
            Email = Request.Form["email"]
        };
        
        if (user.IsValid())
        {
            message = "用户信息提交成功!";
        }
        else
        {
            message = "请填写所有字段。";
        }
    }
}

<form method="post">
    <input type="text" name="name" placeholder="姓名" />
    <input type="text" name="email" placeholder="邮箱" />
    <button type="submit" name="submit">提交</button>
</form>

<p>@message</p>

在这个例子中,UserModel 类验证用户输入的姓名和邮箱是否有效,页面处理表单提交并显示相应的消息。


🔧 类的作用与最佳实践

  1. 封装业务逻辑:将表单验证、数据库操作等封装在类中,使页面代码更简洁、可读性更高。
  2. 提高可重用性:通过类,你可以在多个页面之间共享相同的逻辑,避免重复代码。
  3. 组织项目结构:将类放在 App_Code 文件夹中,可以更好地组织项目结构并提升代码的可维护性。

💡 总结

ASP.NET Web Pages 支持将类嵌入到 Razor 页面中,或者将类定义在独立的文件中。通过类的使用,你可以:

  • 封装复杂的逻辑
  • 提高代码的可读性和可重用性
  • 使页面代码更简洁、组织更清晰

🌐 出站链接推荐


📚 参考资料

  1. 《ASP.NET Web Pages 入门与进阶》
  2. Microsoft 官方文档 – Web Pages 编程指南
  3. 《ASP.NET Web Pages:结构与组织》