iT邦幫忙

2023 iThome 鐵人賽

DAY 12
0
SideProject30

30天挑戰:從無到有,使用Vue.js和.NET建立一個Web登入系統系列 第 12

Day 12:.NET CRUD開發篇 - 如何開發一隻查詢 API(R)

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20230927/20141088bcdBCuVTLP.png

Controllers>UserController.cs

[HttpGet]
public async Task<IActionResult> GetUser([FromQuery]GetUserDto getUser)
{
    var result = await _userService.GetUser(getUser);
    return result;
}

Dtos>UserDto.cs

public class GetUserDto
{
    [EmailAddress]
    [MaxLength(100)]
    [DefaultValue("XXX@gmail.com")]
    public string? Email { get; set; }
}

public class GetUserResponseDto
{
    public string? Username { get; set; }

    public string? Phone { get; set; }
}

Services>UserService.cs

public async Task<IActionResult> GetUser(GetUserDto getUser)
{
    return new JsonResult(new { status = "0" });
}

public async Task<IActionResult> GetUser(GetUserDto getUser)
{
    var result = _context.UserProfiles.Where(e => e.Email.Equals(getUser.Email))
        .Select(e => new GetUserResponseDto
        {
            Username = e.Username,
            Phone = e.Phone
        });
    return new JsonResult(new { status = "0", UserInfo = result });
}

點擊「開始」
https://ithelp.ithome.com.tw/upload/images/20230927/201410886bREhamUZ8.png


上一篇
Day 11:.NET CRUD開發篇 - 如何開發一隻新增 API(C)
下一篇
Day 13:.NET CRUD開發篇 - 如何開發一隻更新 API(U)
系列文
30天挑戰:從無到有,使用Vue.js和.NET建立一個Web登入系統19
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言