###### tags: `Asp.net Core`
# ASP.net Core 5 筆記
## 套件 AutoMapper (此非官方套件,非必用)
由NuGet下載 AutoMapper (AutoMapper.Extensions.Microsoft.DependencyInjection)
建立類別後引用 using AutoMapper;
**class位置 繼承 ":Profile"**
## IActionResult 介面 、HTTP狀態碼
參考影片
- [凱哥寫程式 ASP.NET Core .NET 5 打造WebAPI入門教學(3_6) - 回應正確的HTTP狀態碼?](https://www.youtube.com/watch?v=GUjnwK0BsN4&list=PLneJIGUTIItsqHp_8AbKWb7gyWDZ6pQyz&index=16)
參考網址
- [ IActionResult ](https://docs.microsoft.com/zh-tw/dotnet/api/microsoft.aspnetcore.mvc.iactionresult?view=aspnetcore-5.0)
- [ HTTP狀態碼 ](https://docs.microsoft.com/zh-tw/dotnet/api/system.net.httpstatuscode?view=net-5.0)
## API
參考影片
### 凱哥寫程式ASP.NET Core .NET 5 Web API 入門教學
#### 4取得資料GET
- [影片連結](https://www.youtube.com/watch?v=O1bT1qNSoyw&list=PLneJIGUTIItsqHp_8AbKWb7gyWDZ6pQyz&index=16)
#### 製作API
- **在controller下新建API控制項**
1. get 撈取所有資料
``` csharp=1
[HttpGet]
public IEnumerable<TodoListSelectDto> Get(){
var result = _todoContext.TodoLists.Include(a => a.UpdateEmployee).Include(a => a.InsertEmployee)
.Select(a => new TodoListSelectDto
{
Enable = a.Enable,
InsertEmployeeName = a.InsertEmployee.Name,
InsertTime = a.InsertTime,
Name = a.Name,
Orders = a.Orders,
TodoId = a.TodoId,
UpdateEmployeeName = a.UpdateEmployee.Name,
UpdateTime = a.UpdateTime
});
if (result == null || result.Count() == 0)
{
Response.StatusCode = 404;
}
return _iMapper.Map<IEnumerable<TodoListSelectDto>>(result);
}
```
**註釋**
第1行 宣告方法
第3行 IEnumerable & get 方法
第4-19行 撈取資料的程式碼搭配linq語法
第20行 回傳結果
2. get 撈取單一筆資料
``` csharp=1
[HttpGet("{id}")]
public TodoListSelectDto Get(Guid id){
var result =( from a in _todoContext.TodoLists
join b in _todoContext.Employees on a.InsertEmployeeId equals b.EmployeeId
join c in _todoContext.Employees on a.UpdateEmployeeId equals c.EmployeeId
where a.TodoId == id
select new TodoListSelectDto
{
Enable = a.Enable,
InsertEmployeeName = b.Name,
InsertTime = a.InsertTime,
Name = a.Name,
Orders = a.Orders,
TodoId = a.TodoId,
UpdateEmployeeName = c.Name,
UpdateTime = a.UpdateTime
}).SingleOrDefault();
return result;
}
```
**註釋**
第1行 宣告方法
第3行 Dto名稱 & get 方法 及 指定ID
第4-18行 撈取資料的程式碼搭配linq語法
第19行 回傳結果
#### 4_2關鍵字搜尋
-[影片連結](https://www.youtube.com/watch?v=jUEdwbRMNYE&list=PLneJIGUTIItsqHp_8AbKWb7gyWDZ6pQyz&index=17)
``` csharp=1
#region Get 關鍵字搜尋
[HttpGet]
public IEnumerable<TodoListSelectDto> Get(string name, bool? enable, DateTime? UpdateTime)
{
var result = _todoContext.TodoLists.Include(a => a.UpdateEmployee).Include(a => a.InsertEmployee).Select(a => new TodoListSelectDto
{
Enable = a.Enable,
InsertEmployeeName = a.InsertEmployee.Name,
InsertTime = a.InsertTime,
Name = a.Name,
Orders = a.Orders,
TodoId = a.TodoId,
UpdateEmployeeName = a.UpdateEmployee.Name,
UpdateTime = a.UpdateTime
});
if (!string.IsNullOrWhiteSpace(name))
{
result = result.Where(a => a.Name.Contains(name));
}
if (enable != null)
{
result = result.Where(a => a.Enable == enable);
}
if (UpdateTime != null)
{
result = result.Where(a => a.InsertTime.Date == UpdateTime);
}
return result;
}
#endregion
```
**註釋**
第2行 宣告方法
第3行 Dto名稱 & get 方法 及 配置參數
第5-30行 撈取資料的程式碼搭配linq語法
第32行 回傳結果
- #補充
在參數後面加上問號代表此參數可接受null值
- [參考連結](https://dotblogs.com.tw/charleen/2017/12/26/100359)