用户登录:输入账号和密码,然后判断用户名和密码是否正确,返回对应的处理结果 登录以后将用户名和密码存放在Session中 并在跳转后的页面上从Session中读取用户名和密码,并显示在界面上

    1. 定义接口入参 ```csharp namespace WebApplication5.DTO { public class UserLoginDTO {

      1. public string? username { get; set; }
      2. public string? password { get; set; }
      3. public override string ToString(){
      4. return "username: " + username + " password: " + password;
      5. }

      } }

    1. 2. 定义统一JSON返回格式
    2. ```csharp
    3. namespace WebApplication5.DTO
    4. {
    5. public class Result
    6. {
    7. public Result() { }
    8. public Result(int code,string message, Object result)
    9. {
    10. this.code = code;
    11. this.message = message;
    12. this.result = result;
    13. }
    14. public Result(int code,string message){
    15. this.code = code;
    16. this.message = message;
    17. }
    18. public static Result Success(string message)
    19. {
    20. return new Result(200,message);
    21. }
    22. public static Result Success(string message, Object data)
    23. {
    24. return new Result(200, message, data);
    25. }
    26. public static Result Error(string message)
    27. {
    28. return new Result(500, message);
    29. }
    30. public int code { get; set; }
    31. public string? message { get; set; }
    32. public object? result { get; set; }
    33. public override string ToString()
    34. {
    35. return "code: " + code + " message: " + message + " result: " + result;
    36. }
    37. }
    38. }
    1. 定义登录控制器UserController

      这里为了顺应老师作业的要求,用户名和密码是随便填写的 实际上如果要进行校验

      1. 有数据库 => 执行SQL语句进行判断账号密码是否正确
      2. 无数据库 => 结合代码判断账号和密码是否正确

      对入参进行参数校验,然后将返回结果使用JsonResult包裹进行返回

    1. using Microsoft.AspNetCore.Mvc;
    2. using WebApplication5.DTO;
    3. namespace WebApplication5.controllers;
    4. [ApiController]
    5. [Route("[controller]")]
    6. public class UserLoginController : ControllerBase
    7. {
    8. private readonly ILogger<UserLoginController> _logger;
    9. public UserLoginController(ILogger<UserLoginController> logger)
    10. {
    11. _logger = logger;
    12. }
    13. [HttpPost(Name = "UserLogin")]
    14. public JsonResult doLogin([FromBody] UserLoginDTO userLoginDTO)
    15. {
    16. Console.WriteLine("当前登录用户信息" + userLoginDTO.ToString());
    17. Console.WriteLine("login success");
    18. if(userLoginDTO.username == null && userLoginDTO.password == null){
    19. return new JsonResult(Result.Error("username and password cannot be null"));
    20. }
    21. if(userLoginDTO.username == null){
    22. return new JsonResult(Result.Error("username cannot be null"));
    23. }
    24. if(userLoginDTO.password == null){
    25. return new JsonResult(Result.Error("password cannot be null"));
    26. }
    27. HttpContext.Session.SetString("username", userLoginDTO.username);
    28. HttpContext.Session.SetString("password", userLoginDTO.password);
    29. return new JsonResult(Result.Success("login success"));
    30. }
    31. }
    1. 在前端页面使用Ajax发起请求 ```csharp @page @model IndexModel @{ ViewData[“Title”] = “首页”; } <!DOCTYPE html>

    1. 5. 登录跳转后的页面
    2. ```csharp
    3. @page
    4. @model WebApplication5.Pages.HomeModel
    5. @{
    6. ViewData["Title"] = "页面2";
    7. string? username = this.ViewContext.HttpContext.Session.GetString("username");
    8. string? password = this.ViewContext.HttpContext.Session.GetString("password");
    9. if(username != null && password != null){
    10. <text>用户名: @username</text>
    11. <text>密码:@password</text>
    12. }else{
    13. <text>请先登录</text>
    14. this.ViewContext.HttpContext.Response.Redirect("/");
    15. }
    16. }
    17. <!DOCTYPE html>
    18. <html lang="zh-cn">
    19. <head>
    20. <meta charset="utf-8" />
    21. <style>
    22. .pb-3{
    23. font-size: 40pt;
    24. color: blue;
    25. }
    26. </style>
    27. </head>
    28. <body>
    29. <div></div>
    30. </body>
    31. </html>
    32. <script type="text/javascript">
    33. </script>

    从页面的请求路径中获取参数 在ASP.NET下,可以通过Request.QueryString[“id”] 来获取传入的参数,但是在 ASP.NET Core 下是会报错的 需要改为: HttpContext.Request.Query[“id”] 来获取

    1. @page
    2. @model WebApplication5.Pages.AboutModel
    3. @{
    4. ViewData["Title"] = "页面3";
    5. string username = HttpContext.Request.Query["username"];
    6. string password = HttpContext.Request.Query["password"];
    7. if(username != null && password != null){
    8. <text>用户名: @username</text>
    9. <text>密码:@password</text>
    10. }else{
    11. <text>请先登录</text>
    12. this.ViewContext.HttpContext.Response.Redirect("/");
    13. }
    14. }
    15. <!DOCTYPE html>
    16. <html lang="zh-cn">
    17. <head>
    18. <meta charset="utf-8" />
    19. <style>
    20. .pb-3{
    21. display: flex;
    22. align-items: center;
    23. justify-content: center;
    24. font-size: 40pt;
    25. color: blue;
    26. }
    27. </style>
    28. </head>
    29. <body>
    30. <div></div>
    31. </body>
    32. </html>
    33. <script type="text/javascript">
    34. </script>