發表文章

目前顯示的是有「Asp.Net Core」標籤的文章

[Middleware] Using Middleware in Asp.Net Core

[Middleware] Using Middleware in Asp.Net Core 因為在 Asp.Net Core 中,它本身是一個管線的概念,Middleware 本身就是坐落在管線之中,所以我們可以透過 Middleware , 一直在管線中,安插我們想要執行的一些程式邏輯。 Middleware Middleware 基本上需要注入 RequestDelegate,也需要實作 Invoke 方法。有一點需要注意的是,在 Invoke 中,一定要呼叫 RequestDelegate 方法, 否則管線運行時,運行到此 middleware 就會停止於目前的管線中。 public class MyMiddleware { public MyMiddleware(RequestDelegate next) { _next = next; } public RequestDelegate _next { get; } public async Task Invoke(HttpContext context) { Console.WriteLine("Hello, I am in Middleware!"); await _next(context); } } Configure // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 將 Middleware 排入 管線中 app.UseMiddleware<MyMiddleware>(); app.UseMvc(); } Result MiddlewareExam...

[Swagger&ExtensionService] Adding Swagger and Extensions to ASP.NET Core Web API using XML Documentation

Adding Swagger and Extensions to ASP.NET Core Web API using XML Documentation 之前一直在想,很多服務與設定檔都集中在 Startup.cs 裡面,可是東西服務很多,整個 Configure 與 ConfigureServices 就會越來越肥,但是很不喜歡這樣,很想把類似相關聯的東西集中在一起。Google 看看,做筆記一些筆記,順便研究該怎麼把那些東西從 Startup 中抽離出來。 Startup Startup 類似是以前 ASP.NET 的 Global.asax。它掌管整個系統的設定檔,與要運行那些服務到管線中。它主要分成兩個部分,一個是 Configure 與 ConfigureServices。 ConfigureServices ConfigureServices 主要是掌管註冊服務到的ASP.NET Core的服務容器。 // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } Configure Configure 主要是將要運行的程式或是路由一些設定到後續程式運行的管線中。 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { ...

[JWT] Bearer Token in Asp.Net Core

[JWT] Bearer Token in Asp.Net Core JWT (Json Web Token),主要是為了保護網路安全傳輸的資訊,透過 JWT 可以產生一把權杖,透過這把權杖,來驗證使用者的資訊,細節可以參考附錄4。 JWT Token (權杖) 權杖主要由三個部分所組成(Header, Payload,以及Signature)。 {{"alg":"HS256","typ":"JWT"}.{"nameid":"ROBERT.CHEN@RFF.COM","http://schemas.microsoft.com/ws/2008/06/identity/claims/role":"Admin","exp":1532966362,"iss":"Robert Chen","aud":"Robert For Fun"}} "token" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJST0JFUlQuQ0hFTkBSRkYuQ09NIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiQWRtaW4iLCJleHAiOjE1MzI5NjYzNjIsImlzcyI6IlJvYmVydCBDaGVuIiwiYXVkIjoiUm9iZXJ0IEZvciBGdW4ifQ.5wl6YqmVI9IHSDSgFw_i1bW5OpZuiKGJ9s38V0HiGog" Header 表頭 表頭主要定義演算法,與權杖的類別。 {"alg":"HS256","typ":"JWT"} 上面的內容是指 HMACSHA256 演算法,權證的類別為"JWT" Payload ...

[ORM] Using Dapper async in asp.net core 2.1

[ORM] Using Dapper async in asp.net core 2.1 Dapper 是一套輕量化 ORM 套件,有優秀的效能,支援物件對應,與快速查詢,...,等。 Dapper 優點 查詢快速 優秀的效能 支援物件映射 支援預存程序 SP 支援 Bulk 處理 支援強型別 支援動態綁定 支援參數化查詢 支援例舉 Packages Please install Dapper Nuget PM> Install-Package Dapper -Version 1.50.5 建立 Model public class Employee { public int Id {get;set;} public string Name {get;set;} public Datetime birthday {get;set;} } Repository Pattern IRepository public interface IRepository<T> { Task<List<T>> GetAll(); Task<T> GetById(int id); } XXXRepository public class EmployeeRepository : IRepository<Employee> { readonly IConfiguration _config; public EmployeeRepository(IConfiguration config) { this._config = config; } public IDbConnection Connection { get { return new SqlConnection(_config.GetConnectionString("MyConnec...

[ASP.Net Core] Day 1 - Note

ASP.Net Core - Day 1 Setup Environmen VS 2017 Community Dotnet COre 2.1 SQL Server Express SQL Server Management Studio 建立 ASP.NET Core 專案 File => Project => Empty ASP.NET Core 檔案結構 專案裡的檔案,主要以 Program.cs 與 Startup.cs 為主。 程式的進入點從 Program 開始,透過 WebHost ,將 Startup 註冊進去。 $ ls Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 7/23/2018 5:57 PM obj d----- 7/23/2018 5:56 PM Properties d----- 7/23/2018 5:56 PM wwwroot -a---- 7/23/2018 5:56 PM 630 Program.cs -a---- 7/23/2018 5:56 PM 1118 Startup.cs -a---- 7/23/2018 5:56 PM 310 testcore.csproj 主程式中,建立一個 WebHostBuilder ,告訴 WebHost 要從哪一支程式開始 UseStartup 。 public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder Cr...