發表文章

目前顯示的是 5月, 2020的文章

ASP.NET MVC - Creating Views, Controllers from Model Classes

圖片
CategoryId 這會是一個 Foreign Key. 前面的 ? 表示有或沒有都可以 default, EF會用 cascade, 所以可以刪除 foreign key Category 叫做 navigation property. 可以代表別的 entity 設為 virtual 是為了可以使用 Lazy Loading Database Context 這一個 context class 繼承 System.Data.Entity.DbContext 這一個 context class 一個 db 大部分都會有一個 context class 裡面會有很多 DbSet, 這一個 DbSet 也是 Entity Set DbSet == db 裡面的 table DbSet : 表示告訴 EF 用 Product class 代表 db 裡面的 Products table 裡面的其中一個 row [HttpPost] : 如果 request 傳的 method 是 post request [ValidateAntiForgeryToken] : 確保這一個 request 是由 HTML form 所 trigger 的 prevent xss xss : 會從別的 form 傳 request 直接到你的 web application 上 ([Bind(Include = “ID,Name”)] Category category) 當加一個新的 category, 告訴這一個 method 只要包含 Id 和 Name properties prevent overposting attacks overposting attacks 例子 : 假設現在買了一台電腦, 電腦很貴要 1800000, 但是如果使用 overposting attack, 可以在傳送 form 裡面的 price 變成 $18 這一個 view 是遵照 這一個 model 所製造出來 在 CategoriesController 的 Index method 會傳 一連串的 categories 到 Index view 這樣子 view 就可以拿到這一串...

Create a complex data model - ASP.NET MVC with EF Core

圖片
Customize the Data Model 可以使用 Attributes 來建立自己想要的 Model DataType DataType 跟 SQL 的 Data Type 類似, 但是會比 SQL 的 Data Type 有更多種選擇, 在 EF 裡面, 如果有 email 的 input 可以指定 data type 是 DataType.EmailAddress , 如果只想要日期不要時間, 可以使用 DataType.Date DisplayFormat 想要在 View 上呈現的 format 是甚麼, 例如 StringLength 指定 string 最長的長度, database 跟 MVC project 都會被套用 RegularExpression 如果想要消除空白, 那麼可以使用 Regular Expression MaxLength 沒有 Client side 的 validation, 指定最大的長度 Column 1.當 table 的名字跟 model class 不一樣時, 可以使用 Column, [Column(“”)] 裡面裝的會是 table 的名字 2.可以使用 Column 指定 SqlServer 的 data type [Column(TypeName="money")] public decimal Budget { get; set; } Required Required 必須要和 MinimumLength 一起做搭配才會有作用 Display 顯示在 View 的名字 當做完 update 的時候, 要打 migrations add <migration-name> 然後 dotnet ef database update Navigation Property : 跟不同的 table 的關係的 property 一對多的關係 在官方文章上面, 一個 Instructor 可以教很多課, 所以 在 Instructor 的 table 裡面 CourseAssignments 可以是被定義成一個 collection 一對一的關係 一...

Entity Framework Core Migration

Create an Initial Migration dotnet ef migrations add InitialCreate Examine Up and Down methods 使用 migrations add 的時候, EF 會幫你把資料庫的表轉換成 code 並且存在 Migrations 的資料夾裡面, 檔名會是由 timestamp 和 migration name 所產生, 在 file 裡面 Up method 會根據資料庫的 table 創建一個 Data Model Entity Sets, 反之, Down method 會刪除資料庫裡面的 table 如果在 table 已經存在的情況下去 create 第一次的 migration, 可以用來 create database 的 code 會產生出來, 但是這個 code 並不會執行, 因為 database 裡面已經有相對應的 table 了 https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations?view=aspnetcore-3.1 ​

Web API with individual account and local login in ASP.NET Web API 2.2

圖片
Web API with individual account and local login in ASP.NET Web API 2.2 這一篇文章是使用 OAtuh2 OAuth2 的專有名詞: Resource : 想要藏起來的 data Resource server : 把 Resource 存起來的地方 Resource owner : 允許誰能夠進來拿到 Resource Client : 想要 Resource 的人, (web browser) Access Token : 許可證, 表示證明你可以進入拿到 Resource Bearer Token : 許可證的一種, 任何人只要拿到這一個許可證就可以進去拿 Resource, 看卡不看人, 有卡就可以 Authorization server : 發放 Access Token 的 server Local Login Credential Flow 使用者登入帳密 2.Client, 這裡的 Client 是 Web Browser, 把使用者的帳密拿去給 Authorization Server 做比對 3.Authorization Server 做比對完之後, 會發放一個 Access Token 給 Client 4.Client 再把這一個 Access Token 塞到 Http Request 的 header 來拿到這一個機密的 Resource Individual Accounts 如果選擇 Individual Accounts 在 Web API 的 Authentication, 專案會有一個比對 User Credentials 和 Access Token 的Authentication Server, 在這一個時候, Web API Controller所扮演的腳色就是上述所說的 Resource server (把資料存起來的地方), Authentication Filter 會比對 Access Token, 以及被 [Authorize] 所保護的 classes 或者是 actions, 當 Controller 的 class 有 [Authorize] 這一個 Attribute, 每一個 request...

ASP.NET Core 3.1 - Upload an image

圖片
上傳圖片 snippets // get the image var files = HttpContext.Request.Form.Files; if (files != null) { string uploadFolder = Path.Combine(_hostingEnvironment.WebRootPath, @"img"); // create an unique file name using guid and the file string uniqueFileName = Guid.NewGuid().ToString() + files[0].FileName; string filePath = Path.Combine(uploadFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await files[0].CopyToAsync(fileStream); product.Photo = uniqueFileName; } } 在要上傳的 form 加上 enctype=”multipart/form-data”, 要不然會有 bug ​

ASP.NET Web API - Exception Logger

圖片
在 handle exception 的時候, 有時候可能要 log exception message, 所以我們可以自己 override ExceptionLogger 之後一定要去 WebApiConfig.cs register 這一個 method ​

ASP.NET Wen API - CORS

圖片
因為有安全的問題, 所以都不會允許不同 domain 的 request data, 如果想要別人也可以用你的資料, 就必須要用 CORS 去 NuGet Package Manager install Microsoft.AspNet.WebApi.Cors 在 WebApiConfig.cs register CORS 在想要使用 Cors 的 API Controller 的 class EnableCors 這裡可以看到有 3 個參數, 第一個是 domain, 第二個是 headers, 第三個是 methods (GET, Post…) 如果不想要某一個 method 被使用, 可以 DisableCors ​

ASP.NET Web API - DTOs

圖片
大部分來說, 想要拿到 DB裡面的資料可以直接去 DB拿, 但是這樣會有缺點, 假設資料庫有密碼, 地址, 這一些比較隱密的資訊, 而且這一些資料並不需要透過 API 傳給其他的服務, 那麼直接去 資料庫拿資料就不會是一個好方法 所以DTO就是先過濾掉有需要的資料給其他的 Services,然後再把這一些資料給需要的 Clients 定義 A DTO is an object that defines how the data will be sent over the network. GET: AutoMapper 上面的例子是我們手動產生 DTO 的 class, 但是在真實世界中會用 AutoMapper 來自動產生我們所想要的 DTO ​