ASP--Active Server Pages Summary

本文介绍了ASP的基础概念及其与浏览器服务器(BS)架构的关系,并对比了ADO与ADO.NET的主要区别,探讨了Web服务的工作原理及控件的使用,强调了不断学习的重要性。

一、不断更新

      ASP是在BS学习中接触的,当然和BS有着千丝万缕的关系。BS浏览器服务器,我们利用ASP这个工具可以前台的页面和后台的实现分离开来,从而可以做我们自己的网页。这里还是想啰嗦一下,ASP的全拼是Active Server Pages,动态服务器页面,是强大的web服务器。


二、概况

      自己学习asp这块的内容,如果不总结的感受就是遍地沙子,没有归类,就不好吸收,总结也不是想着自己可以掌握到什么程度,我是想着给自己学的东西一个归属感,帮助自己在之后的学习中更加的清楚。一下是自己的一个对ASP学习的一个理解:

这里写图片描述


2.1 HTTP

      HTTP:HyperText Transfer Protocol,超文本传输协议,是互联网上应用最广泛的一种协议,我们所有的万维网文件都必须遵守这个标准,这里是传送门,就是想告诉大家HTTP是我们做好的网页,别人可以正常访问所要遵循的一个标准。


2.2 ADO和ADO.NET的区别

      其实上面的导图是有点问题的,准确的话应该是ADO.NET的一个分支。ADO.NET是在ADO的基础上,又进一步的扩展,ADO.NET是ADO的后继版本,主要目的是在.NET Framework中更容易地创建分布式、数据共享的应用程序,它提供了一个数据访问接口,以便和OLE DB数据源进行通信,如SQL SERVER.应用程序可以使用ADO.Net连接这些数据源,并检索、处理和更新数据。
  下面是他们的一些区别(参考):

这里写图片描述

        下面是他们的一些区别(参考):为了更好的认识他们,我们引入代码:在ADO中,我们可以结合红皮书和第一次机房:

Private Sub GetUser()  
        Dim strSQL As String  
        Dim strConn As String  
        Dim objRS As ADODB.Recordset  
        Dim strResult As String  

        '建立连接  
        strConn = "provider=SQLOLEDB;Initial Catalog=CESHI;" _  
            & "data source=(local);user id=sa;password=123456"  
        strSQL = "select * from btuser"  

        '创建一个Recordset实例  
        objRS = New ADODB.Recordset  
        With objRS  
            .cursorlocation = adUserClient  
            .CursorType = adOpenStatic  
            .open(strSQL, strConn)  
            Do Until .EOF  
                strResult = .Fields("firstname").value _  
                    & "" & .Fields("Lastname").value  
                .movenext()  

            Loop  
        End With  
        objRS = Nothing  


    End Sub 

        在ADO.NET中,我们一直在用这个,应该比较熟:

        Dim strSQL As String  
        Dim strConn As String  
        Dim objDA As SqlClient.SqlDataAdapter  
        Dim objDS As New Data.DataSet  
        Dim inCounter As Integer  
        Dim strResult As String  
        Dim objcommand As New SqlCommand  


        strConn = "initial catalog=CESHI;data source=(local);" _  
            & "user id=sa;password=123456"  

        strSQL = "select * from tbuser"  
        objDA = New SqlDataAdapter(strSQL, strConn)  
        objDA.Fill(objDS)  

        With objDS.Tables(0)  
            For inCounter = 0 To .Rows.Count - 1  
                strResult = .Rows(inCounter).Item("username").ToString _  
                    & "" & .Rows(inCounter).Item("password").ToString  
                MessageBox.Show(strResult)  

            Next  


        End With 

        通过对比我们发现,他们的连接字符串和sql语句的用法很相似,区分就在内存上了,是Recordset还是DataSet了。


2.3 web服务

      这个是自己比较好奇,他本身已经封装好了,所有他的一个显著的特征就是接口,他有外漏的接口让我们访问,我们不必知道他的内部是如何实现(尽管想知道的话也可以看到),所以他的内容是通过接口对外访问的。新建一个webService如图所示:

这里写图片描述

运行,我们直接点击操作名称就可以进入了,这里我们可以认为是对外访问的接口。

这里写图片描述

这些内容,是如何实现的呢?其实就是一个个的方法,就像酱紫:

 [WebMethod]
        public string AtoB(int sum)
        {
            try 
                {           
                     //从a中减掉500
                    this.decrease(sum);
                    //从b中加上500
                    this.increase(sum);
                    return ("成功");
                }
                catch (Exception)
                {

                    return ("失败");
                }

        }

      分享这个的目的,是感觉特别贴合我们学习的面向对象的这个主线,让我不断的体会。


2.3控件

      控件的使用我们不断的领会事件驱动,也不知道是否片面控件可以让这个页面变得有生命力,对于控件自己有详细总结,感兴趣可以阅读。


三、不断学习

      我也知道这些只是毛毛雨了,那又如何呢,我们的学习没有,总结也没有完,我们要做的就是不断的学习,不断的总结,不断的分享(一不小心就站在巨人的肩膀上了^~^)

ChangePassword.cshtml页面上的代码是这样@page @model UserManagementSystem.Web.Areas.Identity.Pages.Account.Manage.ChangePasswordModel @{ ViewData["Title"] = "修改密码"; } <h3>@ViewData["Title"]</h3> <style> .dark-input { color: grey !important; /* 纯黑色文字 */ font-weight: 500; /* 中等加粗(可选:600 更明显) */ caret-color: #000; /* 光标颜色同步,提升体验 */ } </style> <div class="row"> <div class="col-md-6"> <form method="post"> <div asp-validation-summary="ModelOnly" class="text-danger" role="alert"></div> <div class="form-floating mb-3"> <input asp-for="Input.OldPassword" class="form-control " autocomplete="current-password" aria-required="true" /> <label asp-for="Input.OldPassword" class="dark-input" >旧密码</label> <span asp-validation-for="Input.OldPassword" class="text-danger "></span> </div> <div class="form-floating mb-3"> <input asp-for="Input.NewPassword" class="form-control" autocomplete="new-password" aria-required="true" /> <label asp-for="Input.NewPassword" class="dark-input">新密码</label> <span asp-validation-for="Input.NewPassword" class="text-danger"></span> </div> <div class="form-floating mb-3"> <input asp-for="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" /> <label asp-for="Input.ConfirmPassword" class="dark-input">确认新密码</label> <span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span> </div> <button type="submit" class="w-100 btn btn-lg btn-primary">更新密码</button> </form> </div> </div> @section Scripts { <partial name="_ValidationScriptsPartial" /> }
10-07
@model List<UserManagementSystem.Web.Models.ApplicationUser> @{ ViewData["Title"] = "用户管理"; } <h2>👥 用户管理</h2> @if (TempData["Success"] != null) { <div class="alert alert-success">@TempData["Success"]</div> } @if (TempData["Error"] != null) { <div class="alert alert-danger">@TempData["Error"]</div> } <div class="mb-3 d-flex justify-content-between"> <a asp-action="CreateUser" class="btn btn-primary">➕ 添加用户</a> <form method="get" class="form-inline"> <input type="text" name="search" value="@ViewBag.Search" class="form-control me-1" placeholder="搜索用户名/邮箱/真实姓名/村ID" style="width: 250px;" /> <button type="submit" class="btn btn-outline-secondary">🔍 搜索</button> </form> </div> <table class="table table-striped table-hover"> <thead class="table-dark"> <tr> <th>用户名</th> <th>邮箱</th> <th>真实姓名</th> <th>所属村ID</th> <th>账户状态</th> <th>操作</th> </tr> </thead> <tbody> @foreach (var user in Model) { <tr> <td>@user.UserName</td> <td>@user.Email</td> <td>@(string.IsNullOrEmpty(user.RealName) ? "-" : user.RealName)</td> <td>@user.CmbId</td> <td> @if (user.IsActive) { <span class="badge bg-success">启用</span> } else { <span class="badge bg-secondary">禁用</span> } </td> <td> <a asp-action="EditUser" asp-route-id="@user.Id" class="btn btn-sm btn-info" title="编辑用户信息"> <i class="bi bi-pencil"></i> 编辑 </a> <a asp-action="ResetPassword" asp-route-id="@user.Id" onclick="return confirm('确定要为用户 @user.UserName 重置密码吗?');" class="btn btn-warning btn-sm" title="重置用户密码"> <i class="bi bi-key"></i> 重置密码 </a> @if (user.Email != "admin@example.com") { <form asp-action="DeleteUser" method="post" style="display:inline;" onsubmit="return confirm('确定要删除用户 @user.Email 吗?此操作不可恢复!');"> <input type="hidden" name="id" value="@user.Id" /> <button type="submit" class="btn btn-sm btn-danger" title="删除用户"> <i class="bi bi-trash"></i> 删除 </button> </form> } else { <small class="text-muted">(系统账户)</small> } </td> </tr> } </tbody> </table> <!-- 分页 --> <nav aria-label="用户分页" class="mt-4"> <ul class="pagination justify-content-center"> @for (int i = 1; i <= ViewBag.TotalPages; i++) { <li class="@(i == ViewBag.CurrentPage ? "page-item active" : "page-item")"> <a class="page-link" asp-action="Users" asp-route-search="@ViewBag.Search" asp-route-page="@i" asp-route-pageSize="@ViewBag.PageSize"> @i </a> </li> } </ul> </nav> <!-- 添加分页大小选择 --> <div class="d-flex justify-content-end mt-3"> <div class="btn-group" role="group"> <a asp-action="Users" asp-route-search="@ViewBag.Search" asp-route-page="1" asp-route-pageSize="10" class="btn @(ViewBag.PageSize == 10 ? "btn-primary" : "btn-outline-primary")">10条/页</a> <a asp-action="Users" asp-route-search="@ViewBag.Search" asp-route-page="1" asp-route-pageSize="20" class="btn @(ViewBag.PageSize == 20 ? "btn-primary" : "btn-outline-primary")">20条/页</a> <a asp-action="Users" asp-route-search="@ViewBag.Search" asp-route-page="1" asp-route-pageSize="50" class="btn @(ViewBag.PageSize == 50 ? "btn-primary" : "btn-outline-primary")">50条/页</a> </div> </div>这个页面点击增加用户按钮后 An unhandled exception occurred while processing the request. InvalidOperationException: The view 'CreateUser' was not found. The following locations were searched: /Views/Admin/CreateUser.cshtml /Views/Shared/CreateUser.cshtml /Pages/Shared/CreateUser.cshtml Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable<string> originalLocations)
10-05
@page @model RegisterModel @{ ViewData["Title"] = "Register"; } <h1>@ViewData["Title"]</h1> <div class="row"> <div class="col-md-4"> <form id="registerForm" asp-route-returnUrl="@Model.ReturnUrl" method="post"> <h2>Create a new account.</h2> <hr /> <div asp-validation-summary="ModelOnly" class="text-danger" role="alert"></div> <div class="form-floating mb-3"> <input asp-for="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="name@example.com" /> <label asp-for="Input.Email">Email</label> <span asp-validation-for="Input.Email" class="text-danger"></span> </div> <div class="form-floating mb-3"> <input asp-for="Input.Password" class="form-control" autocomplete="new-password" aria-required="true" placeholder="password" /> <label asp-for="Input.Password">Password</label> <span asp-validation-for="Input.Password" class="text-danger"></span> </div> <div class="form-floating mb-3"> <input asp-for="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" placeholder="password" /> <label asp-for="Input.ConfirmPassword">Confirm Password</label> <span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span> </div> <button id="registerSubmit" type="submit" class="w-100 btn btn-lg btn-primary">Register</button> </form> </div> <div class="col-md-6 col-md-offset-2"> <section> <h3>Use another service to register.</h3> <hr /> @{ if ((Model.ExternalLogins?.Count ?? 0) == 0) { <div> <p> There are no external authentication services configured. See this <a href="https://go.microsoft.com/fwlink/?LinkID=532715">article about setting up this ASP.NET application to support logging in via external services</a>. </p> </div> } else { <form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal"> <div> <p> @foreach (var provider in Model.ExternalLogins!) { <button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button> } </p> </div> </form> } } </section> </div> </div> @section Scripts { <partial name="_ValidationScriptsPartial" /> } 这是注册页面,请补齐字段
10-05
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated> #nullable disable using System; using System.Collections.Generic; namespace WebApplication4.Models; public partial class User { public int ID { get; set; } public string UserName { get; set; } public string Password { get; set; } public string RealName { get; set; } /// <summary> /// 是否有效 /// </summary> public string IsActive { get; set; } public DateTime? CreateTime { get; set; } public DateTime? LastLoginTime { get; set; } /// <summary> /// 盐值 /// </summary> public string Salt { get; set; } public string Role { get; set; } public bool? CanCreate { get; set; } public bool? CanEdit { get; set; } public bool? CanDelete { get; set; } public bool? CanManage { get; set; } } 这是我的User.cs, 请给完善下面这个编辑用户的页面@page @model WebApplication4.Areas.Identity.Pages.Admin.Users.EditModel @{ ViewData["Title"] = "编辑用户"; Layout = "_Layout"; @* 或者使用默认布局 *@ } <h1>@ViewData["Title"]</h1> <form method="post"> <!-- 防止跨站请求伪造攻击(CSRF) --> @Html.AntiForgeryToken() <!-- 保存用户 ID,用于 POST 提交 --> <input type="hidden" asp-for="Input.Id" /> <div class="form-group mb-3"> <label asp-for="Input.UserName" class="form-label"></label> <input asp-for="Input.UserName" class="form-control" /> <span asp-validation-for="Input.UserName" class="text-danger"></span> </div> <div class="form-group mb-3"> <label asp-for="Input.Email" class="form-label"></label> <input asp-for="Input.Email" class="form-control" /> <span asp-validation-for="Input.Email" class="text-danger"></span> </div> <button type="submit" class="btn btn-primary">保存更改</button> <a asp-page="./Index" class="btn btn-secondary">返回列表</a> </form> @section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); // 启用客户端验证 } } 下面是编辑用的后台代码using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace WebApplication4.Areas.Identity.Pages.Admin.Users { [Authorize(Roles = "Admin")] public class EditModel : PageModel { private readonly UserManager<IdentityUser> _userManager; public EditModel(UserManager<IdentityUser> userManager) { _userManager = userManager; } [BindProperty] public InputModel Input { get; set; } public class InputModel { public string Id { get; set; } public string Email { get; set; } public string UserName { get; set; } } // GET: /Identity/Admin/Users/Edit/1 public async Task<IActionResult> OnGetAsync(string id) { if (string.IsNullOrEmpty(id)) return BadRequest(); var user = await _userManager.FindByIdAsync(id); if (user == null) return NotFound(); Input = new InputModel { Id = user.Id, Email = user.Email, UserName = user.UserName }; return Page(); } // POST: /Identity/Admin/Users/Edit/1 public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid) return Page(); var user = await _userManager.FindByIdAsync(Input.Id); if (user == null) return NotFound(); user.Email = Input.Email; user.UserName = Input.UserName; var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { return RedirectToPage("./Index"); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return Page(); } } }
10-04
评论 81
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值