SlideShare a Scribd company logo
1 of 42
11/29 台北,12/06 高雄,12/20 台中
進入 ASP.NET MVC 5 的世界
小朱

MS MVP on Windows Azure
台灣微軟資深講師
Agenda
• 由 Web Form 開發人員的觀點看 MVC。
• ASP.NET MVC 概觀。

• MVC5 值得關注的新玩意。
• One ASP.NET
• MVC5 新功能
• Web API 2 新功能
• ASP.NET Identity
課程目標
• 經過本課程後,您可以…
• 說明 MVC 的概念。

• 了解 ASP.NET MVC 的架構。
• 了解 One ASP.NET 的概念。
• 了解 MVC 5, Web API 2 的新功能。

• 了解 ASP.NET Identity 的架構。
MVC 是啥?
• 一種程式設計典範。
• 1970 年代就有了。

• 不是用來找你麻煩的。
• 更清楚的程式職責。
• 可測試性 (Testability)。

• 習慣的改變。
• 這是 Web Form 開發人員進
階的關卡。
Web Form 的角度…
• MVC 應用程式沒有控制項。
• ASP.NET Web Form 有一堆控制項。

• 必須聽命於控制項。

• MVC 的職責分明。
• ASP.NET Web Form 的職責混合 (Page_Load Only…)

• 要付出很多的心血才能職責分明。

• MVC 不好學。
• 其實是因為積習難改。
什麼是 ASP.NET MVC?
• 以 ASP.NET 技術實作的 MVC Pattern。
• 2007 年底開始研發,2009 年才釋出首版。

• 最新版為 5.0。

• 職責分明
• Model, View, Controller,之間互不相依。

• 比 Web Form 更高的彈性。

• 相容於現有的 ASP.NET 基本功能。
• 可客製化程度更高。
為什麼要用 ASP.NET MVC?
• 習慣取代配置 (Convention over Configuration)。
• 依規則配置程式即可。

• 輕量化:
• 拋棄肥大的 ViewState。
• 套版時比 Web Form 更快更輕鬆。
• 強型別的支援 (coding by Intellisense)。

• 物件導向的強化:
• 職責分明。
• 更具彈性。
• 可測試。
習慣取代配置
• 專案結構
• 東西該擺哪就擺哪。

• 容易尋找。

• 命名規則
• Controller 的命名一定要有

“Controller”。
• 共用的 View 一定要用 “_” 開頭。
demo
• MVC 5 專案
Model
• Model 是資料來源地。
• 資料庫,資料存取層或資料服務。

• 不是一定要用 Entity Framework!
• ADO.NET DataSet/DataTable 一樣能用。
• POCO 隔離法 (將資料結構和資料存取行為分離)。

• 特定行為的 Model
• ViewModel - 給特定的 View 使用。
• 在 Controller 內做 Model 與 ViewModel 的資料交換。
Controller
• 接取要求訊息,查詢或繫結 Model,將結果丟給 View。
• 必須繼承自 Controller 類別。

• 定義動作 (Action)
• 處理要求。
• 決定回傳的類型。
• 一般 View
• 檔案下載
• 重導向
• …
Controller 與 Model 互動
• Model Binding
• 參數的值,由 MVC 核心自動由要求接取。

• 預設 binding 已可支援多數需求。

• Model Validation
• 由 Model 端定義驗證規則。

• 由 ModelState.IsValid 確認資料的完整與有效性。
• 伺服器端檢查
• ModelState.AddModelError() 新增驗證錯誤。
demo
• 簡單的 Controller/Model 互動。
View
• 接取 Controller 的資料,呈現給使用者。
• 沒有 Designer,只有 HTML Editor。
• 套版容易。
• 必須要了解 HTML 的結構。
• 在 HTML 內套用資料。
View
• 資料來源
• Model: 由 Controller 中的 View() 方法的參數值取得。

• ViewBag: 由 Controller 定義屬性資訊後取得。
• ViewDataDictionary: 由 Controller 定義屬性資訊後取得,
屬於鍵值對資料。

• 強型別的 Model 資源。
• @model 定義型別。
View
• HTML Helpers
• 輔助產生 HTML tags。
• 表單宣告 (Html.BeginForm())。
• 表單控制項 (Html.DropDownList(), Html.CheckBox(),
Html.TextBox(), …)

• 產生連結參數 (Html.ActionLink())。

• 部份檢視 (Html.RenderAction(), Html.RenderPartial())。
demo
• 簡單的 Controller/Model/View 互動。
ASP.NET Routing
• MVC 運作順暢的核心元件。
• 由 URL 決定跑哪個 controller, 哪個 action。

• 有預設值。
http://www.adworks.com/photo/display/1
Default Route
Controller

Action

ID
ASP.NET Routing
ASP.NET Routing
• 可變長度的 URL 參數
• query/{queryname}/{*queryvalues}
Area
• 更大型的 Web 應用。
• 以 Area 來切份系統區塊。
• 每個 Area 下有各自的 controller, view, model 等。
• 由 Routing 來決定進入哪個 area。
demo
• ASP.NET Routing
ASP.NET MVC 5
• 改變不小,但其實也不大。
• One ASP.NET

• Scaffolding, 驗證過濾器, 屬性路由, …
• Web API 2
• ASP.NET Identity
One ASP.NET
One ASP.NET
One ASP.NET
One ASP.NET
• 未來只會有一個 ASP.NET Core Service
• 不論是 Web Form, MVC, Web API, SPA 等都以同一個基

礎發展。
• 所有 ASP.NET 內的核心功能,Web Form, MVC, Web API,
SPA 等都支援,不分類型。
• Visual Studio 內的範本更乾淨。
• 開發人員可基於 ASP.NET Core Service 開發新服務類型。
OWIN (Open Web Interface for .NET)
• One ASP.NET 的基石。
• 中間層軟體的規格 (Middleware)。
• 定義向上 (framework) 的介面。
• 定義向下 (host) 的功能。

• 微軟的 OWIN 實作:Katana Project。
• Web API 2 和 ASP.NET Identity 的基礎之一。
• 可支援 IIS hosting 或 Self-hosting 能力。

• NuGet-enabled。
MVC 5 Scaffolding
• 翻譯成 “支架”。
• 所有 ASP.NET MVC 該有的元件都能在此找到。
• 簡化新增 Controller 和 View 的流程。
• 支援 Model scaffolding,自動產生相應欄位。

• 在 Web Form 應用程式中加入 MVC 功能。
• 最小相依。
• 完全相依。
demo
• MVC 5 Scaffolding
MVC 5 的過濾器
• 驗證過濾器 (Authentication Filter)
• 針對特定的 Controller/Action 或整個網站設定驗證的處

理方式。
• 實作 IAuthenticationFilter 介面。
• 繼承自 ActionFilterAttribute 類別。

• 可覆寫式過濾器 (Overridable Action Filter)
• 藉由覆寫來取消特定 Action Filter 的行為。
demo
• MVC 5 Authentication Filter
• MVC 5 Overridable Action Filter
Web API 2
• Web API 是類似 MVC 架構的 RESTful API 實作應用。
• 一樣有 Controller,但繼承自 ApiController。
• 一樣使用 ASP.NET Routing。
• 需要加入 Web API Package (NuGet)。
• 支援更豐富的 HTTP 訊息處理能力。

• Web API 2 支援了…
• 屬性路由 (attribute routing)。
• CORS 的支援。
• 更多… (可參考 What’s New in Visual Studio 2013)。
demo
• 基本的 Web API
• Web API Attribute Routing
• Web API CORS
ASP.NET Identity
• 全新的 ASP.NET 內建驗證與授權機制。
• 以 Entity Framework Code-First 方式編寫。

• 允許自訂欄位。
• 這是 ASP.NET Membership 被罵最慘的項目之一…

• 支援不同的驗證方式 (Windows, Forms, ADFS)。
• 支援 OAuth
• Facebook, Google, Microsoft Account, Twitter
• 由 OWIN 所內建。
自訂會員資料欄位
注意:要先有資料庫。
1.

啟用 Database Migration (Enable-Migration)

2.

修改 Models/IdentityModel.cs ,加入新欄位。

3.

在 Database Migration 中加入新欄位的變更 (Add-Migration)。

4.

更新資料庫 (Update-Database)。

5.

修改 Models/AccountViewModels.cs,加入新欄位的部份。

6.

修改 Views/Register.cshtml,加入新欄位的部份。

7.

修改 AccountController.Register() ,加入新欄位的部份。

8.

執行程式。
http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html
demo
• ASP.NET Identity
• Facebook 驗證
• 自訂會員欄位。
總結
• ASP.NET MVC 是未來中大型應用程式的主流
• 不會也沒必要取代 Web Form。

• 但它的彈性比 Web Form 強太多了。

• MVC 5, Web API 2 等讓實作 MVC / Web API 的應
用程式有了更多的選擇。

• ASP.NET Identity 提供了全新的會員管理架構,並支
援多種驗證方法。
References
• ASP.NET MVC: http://asp.net/mvc
• ASP.NET Web API: http://asp.net/web-api

• What’s new in Visual Studio 2013:
http://www.asp.net/visualstudio/overview/2013/release-notes
• MSDN 台灣部落格上有許多的精采文章。
• twMVC: http://mvc.tw
學習 MVC,你可以選擇…
• 由 6 位 MVP 合作執筆。
• 其中三位是 twMVC 社群

創始人。

• 台灣唯一有介紹 Web
API 且內容超過 200 頁

的中文書。
• 台灣最暢銷的 ASP.NET
MVC 中文書。
Introduction to ASP.NET MVC and MVC 5 Features

More Related Content

What's hot

ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索Will Huang
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練22016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2Duran Hsieh
 
Ch09 整合資料庫
Ch09 整合資料庫 Ch09 整合資料庫
Ch09 整合資料庫 Justin Lin
 
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)Jeff Chu
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練52016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5Duran Hsieh
 
Angular 2 表單的處理與驗證
Angular 2 表單的處理與驗證Angular 2 表單的處理與驗證
Angular 2 表單的處理與驗證Jeff Wu
 
輕鬆上手ASP.NET Web API 2.1.2
輕鬆上手ASP.NET Web API 2.1.2輕鬆上手ASP.NET Web API 2.1.2
輕鬆上手ASP.NET Web API 2.1.2Bruce Chen
 
Study4 love.2016.2.20.ionic
Study4 love.2016.2.20.ionicStudy4 love.2016.2.20.ionic
Study4 love.2016.2.20.ionicKyle Shen
 
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...SernHao TV
 
Team Foundation Server
Team Foundation ServerTeam Foundation Server
Team Foundation Server國昭 張
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Jollen Chen
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Will Huang
 
CodeIgniter 2.0.X
CodeIgniter 2.0.XCodeIgniter 2.0.X
CodeIgniter 2.0.XBo-Yi Wu
 

What's hot (20)

ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索ASP.NET MVC 5 新功能探索
ASP.NET MVC 5 新功能探索
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
Svn process
Svn processSvn process
Svn process
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練22016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
 
Ch09 整合資料庫
Ch09 整合資料庫 Ch09 整合資料庫
Ch09 整合資料庫
 
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
 
Angularjs
AngularjsAngularjs
Angularjs
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練52016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
 
Angular 2 表單的處理與驗證
Angular 2 表單的處理與驗證Angular 2 表單的處理與驗證
Angular 2 表單的處理與驗證
 
Mvc model
Mvc modelMvc model
Mvc model
 
輕鬆上手ASP.NET Web API 2.1.2
輕鬆上手ASP.NET Web API 2.1.2輕鬆上手ASP.NET Web API 2.1.2
輕鬆上手ASP.NET Web API 2.1.2
 
Study4 love.2016.2.20.ionic
Study4 love.2016.2.20.ionicStudy4 love.2016.2.20.ionic
Study4 love.2016.2.20.ionic
 
Views 3
Views 3Views 3
Views 3
 
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
 
Maven技术及诸葛商城应用(1)
Maven技术及诸葛商城应用(1)Maven技术及诸葛商城应用(1)
Maven技术及诸葛商城应用(1)
 
Team Foundation Server
Team Foundation ServerTeam Foundation Server
Team Foundation Server
 
Vue
VueVue
Vue
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)
 
CodeIgniter 2.0.X
CodeIgniter 2.0.XCodeIgniter 2.0.X
CodeIgniter 2.0.X
 

Similar to Introduction to ASP.NET MVC and MVC 5 Features

2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4Duran Hsieh
 
ASP.Net MVC Framework
ASP.Net MVC FrameworkASP.Net MVC Framework
ASP.Net MVC Framework國昭 張
 
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1twMVC
 
How to ASP.NET MVC4
How to ASP.NET MVC4How to ASP.NET MVC4
How to ASP.NET MVC4Daniel Chou
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC
 
Ch13 整合Spring MVC
Ch13  整合Spring MVC Ch13  整合Spring MVC
Ch13 整合Spring MVC Justin Lin
 
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4twMVC
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介Allen Lsy
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例Will Huang
 
微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構Jeff Chu
 
ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索Will Huang
 
Spring mvc
Spring mvcSpring mvc
Spring mvcpepsixp
 
Asp.net mvc 基礎
Asp.net mvc 基礎Asp.net mvc 基礎
Asp.net mvc 基礎Gelis Wu
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練32015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3Duran Hsieh
 
Asp.net mvc 6 新功能初探
Asp.net mvc 6 新功能初探Asp.net mvc 6 新功能初探
Asp.net mvc 6 新功能初探Gelis Wu
 
Ch13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityJustin Lin
 
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛Edward Kuo
 
[DCTPE2010] 如何開發 CCK 欄位模組
[DCTPE2010] 如何開發 CCK 欄位模組[DCTPE2010] 如何開發 CCK 欄位模組
[DCTPE2010] 如何開發 CCK 欄位模組Drupal Taiwan
 

Similar to Introduction to ASP.NET MVC and MVC 5 Features (20)

2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
 
ASP.Net MVC Framework
ASP.Net MVC FrameworkASP.Net MVC Framework
ASP.Net MVC Framework
 
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1與 Asp.net mvc 的第一次親密接觸 - twMVC#1
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
 
How to ASP.NET MVC4
How to ASP.NET MVC4How to ASP.NET MVC4
How to ASP.NET MVC4
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸
 
ASP.net MVC
ASP.net MVCASP.net MVC
ASP.net MVC
 
Ch13 整合Spring MVC
Ch13  整合Spring MVC Ch13  整合Spring MVC
Ch13 整合Spring MVC
 
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
 
微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構微軟實戰課程日:玩轉雲端 技術與架構
微軟實戰課程日:玩轉雲端 技術與架構
 
ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Asp.net mvc 基礎
Asp.net mvc 基礎Asp.net mvc 基礎
Asp.net mvc 基礎
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練32015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
 
Asp.net mvc 6 新功能初探
Asp.net mvc 6 新功能初探Asp.net mvc 6 新功能初探
Asp.net mvc 6 新功能初探
 
Ch13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/Security
 
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
 
[DCTPE2010] 如何開發 CCK 欄位模組
[DCTPE2010] 如何開發 CCK 欄位模組[DCTPE2010] 如何開發 CCK 欄位模組
[DCTPE2010] 如何開發 CCK 欄位模組
 

More from Jeff Chu

.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0Jeff Chu
 
Developing Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive ServicesDeveloping Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive ServicesJeff Chu
 
.NET Innovations and Improvements
.NET Innovations and Improvements.NET Innovations and Improvements
.NET Innovations and ImprovementsJeff Chu
 
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)Jeff Chu
 
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)Jeff Chu
 
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)Jeff Chu
 
Developing serverless applications with azure functions
Developing serverless applications with azure functionsDeveloping serverless applications with azure functions
Developing serverless applications with azure functionsJeff Chu
 
Connect() Mini 2016
Connect() Mini 2016Connect() Mini 2016
Connect() Mini 2016Jeff Chu
 
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016Jeff Chu
 
Windows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree DayWindows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree DayJeff Chu
 
領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)Jeff Chu
 
物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DI物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DIJeff Chu
 
例外處理實務
例外處理實務例外處理實務
例外處理實務Jeff Chu
 
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)Jeff Chu
 
Machine Learning, Big Data, Insights
Machine Learning, Big Data, InsightsMachine Learning, Big Data, Insights
Machine Learning, Big Data, InsightsJeff Chu
 
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...Jeff Chu
 
Cloud computing for manufacturing
Cloud computing for manufacturingCloud computing for manufacturing
Cloud computing for manufacturingJeff Chu
 
Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014Jeff Chu
 
Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)Jeff Chu
 
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)Jeff Chu
 

More from Jeff Chu (20)

.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0.NET Conf 2019 高雄場 - .NET Core 3.0
.NET Conf 2019 高雄場 - .NET Core 3.0
 
Developing Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive ServicesDeveloping Serverless application with Microsoft Azure and Cognitive Services
Developing Serverless application with Microsoft Azure and Cognitive Services
 
.NET Innovations and Improvements
.NET Innovations and Improvements.NET Innovations and Improvements
.NET Innovations and Improvements
 
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
Microsoft Azure IoT 手把手實作 @ K.NET by Maduka (2017-8-12)
 
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
Innovations of .NET and Azure (Recaps of Build 2017 selected sessions)
 
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
利用微軟雲端平台打造完整 IoT 服務鍊 (maduka)
 
Developing serverless applications with azure functions
Developing serverless applications with azure functionsDeveloping serverless applications with azure functions
Developing serverless applications with azure functions
 
Connect() Mini 2016
Connect() Mini 2016Connect() Mini 2016
Connect() Mini 2016
 
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
Windows 與 Azure 的容器旅程 @ Ignite Mini 2016
 
Windows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree DayWindows 與 Azure 的容器旅程 @ Skilltree Day
Windows 與 Azure 的容器旅程 @ Skilltree Day
 
領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)領域驅動設計 (Domain Driven Design)
領域驅動設計 (Domain Driven Design)
 
物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DI物件導向設計原則:SOLID + DI
物件導向設計原則:SOLID + DI
 
例外處理實務
例外處理實務例外處理實務
例外處理實務
 
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
ASP.NET 5 快速入門 (Getting Started ASP.NET 5)
 
Machine Learning, Big Data, Insights
Machine Learning, Big Data, InsightsMachine Learning, Big Data, Insights
Machine Learning, Big Data, Insights
 
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
Global Azure Bootcamp 2015 Taipei: Planning and Migrating your Application fr...
 
Cloud computing for manufacturing
Cloud computing for manufacturingCloud computing for manufacturing
Cloud computing for manufacturing
 
Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014Microsoft Azure 新功能導覽 @ Build 2014
Microsoft Azure 新功能導覽 @ Build 2014
 
Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)Microsoft Azure 概觀 (2014-4-2 雲端達人班)
Microsoft Azure 概觀 (2014-4-2 雲端達人班)
 
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
Microsoft Azure 虛擬機器與虛擬網路 (2014-4-2 雲端達人班)
 

Introduction to ASP.NET MVC and MVC 5 Features

  • 2. 進入 ASP.NET MVC 5 的世界 小朱 MS MVP on Windows Azure 台灣微軟資深講師
  • 3. Agenda • 由 Web Form 開發人員的觀點看 MVC。 • ASP.NET MVC 概觀。 • MVC5 值得關注的新玩意。 • One ASP.NET • MVC5 新功能 • Web API 2 新功能 • ASP.NET Identity
  • 4. 課程目標 • 經過本課程後,您可以… • 說明 MVC 的概念。 • 了解 ASP.NET MVC 的架構。 • 了解 One ASP.NET 的概念。 • 了解 MVC 5, Web API 2 的新功能。 • 了解 ASP.NET Identity 的架構。
  • 5. MVC 是啥? • 一種程式設計典範。 • 1970 年代就有了。 • 不是用來找你麻煩的。 • 更清楚的程式職責。 • 可測試性 (Testability)。 • 習慣的改變。 • 這是 Web Form 開發人員進 階的關卡。
  • 6. Web Form 的角度… • MVC 應用程式沒有控制項。 • ASP.NET Web Form 有一堆控制項。 • 必須聽命於控制項。 • MVC 的職責分明。 • ASP.NET Web Form 的職責混合 (Page_Load Only…) • 要付出很多的心血才能職責分明。 • MVC 不好學。 • 其實是因為積習難改。
  • 7. 什麼是 ASP.NET MVC? • 以 ASP.NET 技術實作的 MVC Pattern。 • 2007 年底開始研發,2009 年才釋出首版。 • 最新版為 5.0。 • 職責分明 • Model, View, Controller,之間互不相依。 • 比 Web Form 更高的彈性。 • 相容於現有的 ASP.NET 基本功能。 • 可客製化程度更高。
  • 8. 為什麼要用 ASP.NET MVC? • 習慣取代配置 (Convention over Configuration)。 • 依規則配置程式即可。 • 輕量化: • 拋棄肥大的 ViewState。 • 套版時比 Web Form 更快更輕鬆。 • 強型別的支援 (coding by Intellisense)。 • 物件導向的強化: • 職責分明。 • 更具彈性。 • 可測試。
  • 9. 習慣取代配置 • 專案結構 • 東西該擺哪就擺哪。 • 容易尋找。 • 命名規則 • Controller 的命名一定要有 “Controller”。 • 共用的 View 一定要用 “_” 開頭。
  • 10. demo • MVC 5 專案
  • 11. Model • Model 是資料來源地。 • 資料庫,資料存取層或資料服務。 • 不是一定要用 Entity Framework! • ADO.NET DataSet/DataTable 一樣能用。 • POCO 隔離法 (將資料結構和資料存取行為分離)。 • 特定行為的 Model • ViewModel - 給特定的 View 使用。 • 在 Controller 內做 Model 與 ViewModel 的資料交換。
  • 12. Controller • 接取要求訊息,查詢或繫結 Model,將結果丟給 View。 • 必須繼承自 Controller 類別。 • 定義動作 (Action) • 處理要求。 • 決定回傳的類型。 • 一般 View • 檔案下載 • 重導向 • …
  • 13. Controller 與 Model 互動 • Model Binding • 參數的值,由 MVC 核心自動由要求接取。 • 預設 binding 已可支援多數需求。 • Model Validation • 由 Model 端定義驗證規則。 • 由 ModelState.IsValid 確認資料的完整與有效性。 • 伺服器端檢查 • ModelState.AddModelError() 新增驗證錯誤。
  • 15. View • 接取 Controller 的資料,呈現給使用者。 • 沒有 Designer,只有 HTML Editor。 • 套版容易。 • 必須要了解 HTML 的結構。 • 在 HTML 內套用資料。
  • 16. View • 資料來源 • Model: 由 Controller 中的 View() 方法的參數值取得。 • ViewBag: 由 Controller 定義屬性資訊後取得。 • ViewDataDictionary: 由 Controller 定義屬性資訊後取得, 屬於鍵值對資料。 • 強型別的 Model 資源。 • @model 定義型別。
  • 17. View • HTML Helpers • 輔助產生 HTML tags。 • 表單宣告 (Html.BeginForm())。 • 表單控制項 (Html.DropDownList(), Html.CheckBox(), Html.TextBox(), …) • 產生連結參數 (Html.ActionLink())。 • 部份檢視 (Html.RenderAction(), Html.RenderPartial())。
  • 19. ASP.NET Routing • MVC 運作順暢的核心元件。 • 由 URL 決定跑哪個 controller, 哪個 action。 • 有預設值。 http://www.adworks.com/photo/display/1 Default Route Controller Action ID
  • 21. ASP.NET Routing • 可變長度的 URL 參數 • query/{queryname}/{*queryvalues}
  • 22. Area • 更大型的 Web 應用。 • 以 Area 來切份系統區塊。 • 每個 Area 下有各自的 controller, view, model 等。 • 由 Routing 來決定進入哪個 area。
  • 24. ASP.NET MVC 5 • 改變不小,但其實也不大。 • One ASP.NET • Scaffolding, 驗證過濾器, 屬性路由, … • Web API 2 • ASP.NET Identity
  • 28. One ASP.NET • 未來只會有一個 ASP.NET Core Service • 不論是 Web Form, MVC, Web API, SPA 等都以同一個基 礎發展。 • 所有 ASP.NET 內的核心功能,Web Form, MVC, Web API, SPA 等都支援,不分類型。 • Visual Studio 內的範本更乾淨。 • 開發人員可基於 ASP.NET Core Service 開發新服務類型。
  • 29. OWIN (Open Web Interface for .NET) • One ASP.NET 的基石。 • 中間層軟體的規格 (Middleware)。 • 定義向上 (framework) 的介面。 • 定義向下 (host) 的功能。 • 微軟的 OWIN 實作:Katana Project。 • Web API 2 和 ASP.NET Identity 的基礎之一。 • 可支援 IIS hosting 或 Self-hosting 能力。 • NuGet-enabled。
  • 30. MVC 5 Scaffolding • 翻譯成 “支架”。 • 所有 ASP.NET MVC 該有的元件都能在此找到。 • 簡化新增 Controller 和 View 的流程。 • 支援 Model scaffolding,自動產生相應欄位。 • 在 Web Form 應用程式中加入 MVC 功能。 • 最小相依。 • 完全相依。
  • 31. demo • MVC 5 Scaffolding
  • 32. MVC 5 的過濾器 • 驗證過濾器 (Authentication Filter) • 針對特定的 Controller/Action 或整個網站設定驗證的處 理方式。 • 實作 IAuthenticationFilter 介面。 • 繼承自 ActionFilterAttribute 類別。 • 可覆寫式過濾器 (Overridable Action Filter) • 藉由覆寫來取消特定 Action Filter 的行為。
  • 33. demo • MVC 5 Authentication Filter • MVC 5 Overridable Action Filter
  • 34. Web API 2 • Web API 是類似 MVC 架構的 RESTful API 實作應用。 • 一樣有 Controller,但繼承自 ApiController。 • 一樣使用 ASP.NET Routing。 • 需要加入 Web API Package (NuGet)。 • 支援更豐富的 HTTP 訊息處理能力。 • Web API 2 支援了… • 屬性路由 (attribute routing)。 • CORS 的支援。 • 更多… (可參考 What’s New in Visual Studio 2013)。
  • 35. demo • 基本的 Web API • Web API Attribute Routing • Web API CORS
  • 36. ASP.NET Identity • 全新的 ASP.NET 內建驗證與授權機制。 • 以 Entity Framework Code-First 方式編寫。 • 允許自訂欄位。 • 這是 ASP.NET Membership 被罵最慘的項目之一… • 支援不同的驗證方式 (Windows, Forms, ADFS)。 • 支援 OAuth • Facebook, Google, Microsoft Account, Twitter • 由 OWIN 所內建。
  • 37. 自訂會員資料欄位 注意:要先有資料庫。 1. 啟用 Database Migration (Enable-Migration) 2. 修改 Models/IdentityModel.cs ,加入新欄位。 3. 在 Database Migration 中加入新欄位的變更 (Add-Migration)。 4. 更新資料庫 (Update-Database)。 5. 修改 Models/AccountViewModels.cs,加入新欄位的部份。 6. 修改 Views/Register.cshtml,加入新欄位的部份。 7. 修改 AccountController.Register() ,加入新欄位的部份。 8. 執行程式。 http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html
  • 38. demo • ASP.NET Identity • Facebook 驗證 • 自訂會員欄位。
  • 39. 總結 • ASP.NET MVC 是未來中大型應用程式的主流 • 不會也沒必要取代 Web Form。 • 但它的彈性比 Web Form 強太多了。 • MVC 5, Web API 2 等讓實作 MVC / Web API 的應 用程式有了更多的選擇。 • ASP.NET Identity 提供了全新的會員管理架構,並支 援多種驗證方法。
  • 40. References • ASP.NET MVC: http://asp.net/mvc • ASP.NET Web API: http://asp.net/web-api • What’s new in Visual Studio 2013: http://www.asp.net/visualstudio/overview/2013/release-notes • MSDN 台灣部落格上有許多的精采文章。 • twMVC: http://mvc.tw
  • 41. 學習 MVC,你可以選擇… • 由 6 位 MVP 合作執筆。 • 其中三位是 twMVC 社群 創始人。 • 台灣唯一有介紹 Web API 且內容超過 200 頁 的中文書。 • 台灣最暢銷的 ASP.NET MVC 中文書。