SlideShare a Scribd company logo
ASP.NET MVC 4 RC 新增功能介紹
ASP.NET MVC 4 就快出來了,你跟上了嗎?
 中文姓名:吳俊毅
 英文姓名:Gelis Wu
 Email: geliswu@wistronits.com
 MSN:wergelis@msn.com
 Blog:http://www.dotblogs.com.tw/gelis/
 專長:.NET 相關 Solution、Web 應用、分散式應用程式
開發、Architecture Design、系統分析/設計。
About Me
MVP Adward 2011/2012
Visual C#
2
ASP.NET MVC 的發展歷史
3
 雖然MVC 4 有些新增功能到了RC的時候被移除掉了,不過
它還是可圈可點之處啦。
 感覺 MVC 4 RTM 好像拖了很久!不過開發人員是辛苦的,
所以我們也不要太計較。
 雖然讓我們等了很久,不過我們相信,我們的等待是值得的
 RTM正式版已於2012/8/15上市了
寫在前面
4
 所使用的開發環境
 如何將 MVC3 的專案升級成 MVC4
 ASP.NET MVC 4 RC 新增功能介紹
大綱
5
 所使用的開發環境
 如何將 MVC3 的專案升級成 MVC4
 ASP.NET MVC 4 RC 新增功能介紹
大綱
6
 Windows 7 Home Premium
 Visual Studio 2010 Ultimate &.NET Framework 4.0
 Visual Studio 2012 Ultimate RC & .NET Framework 4.5
RC
 在Visual Studio 2012 RC 中使用的版本已經是MVC 4.0 RC的
版本
 建議使用 Windows Platform Installer 4.0 來安裝。
 當然也可以下載Visual Studio 2010使用的 MVC 4.0 RC 版本
 或者使用8/15最新Release的Visual Studio 2012 RTM
所使用的開發環境
7
 所使用的開發環境
 如何將 MVC3 的專案升級成 MVC4
 ASP.NET MVC 4 RC 新增功能介紹
大綱
8
 要將 ASP.NET MVC3 轉成 MVC4 絕招、第一式、乾坤大挪
移。
 所謂的乾坤大挪移就是 COPY
 因此我們首先得建立一個空的ASP.NET MVC 4.0 的專案
第一式、乾坤大挪移
9
Demo、使用現有MVC3專案
10
Demo、使用現有MVC3專案
11
Demo、使用現有MVC3專案
12
 首先先建立好一個空的ASP.NET MVC 4.0的應用程式。
步驟一、先搬Model
13
 首先先建立好一個空的ASP.NET MVC 4.0的應用程式。
步驟一、先搬Model
如果Namespace有變
動,記得要修改。
14
步驟二、再搬Controller
15
 不是所有都一起搬過去。
步驟三、續搬View
16
 不是所有都一起搬過去。
步驟三、續搬View
17
 不是所有都一起搬過去。
步驟三、續搬View
18
 不是所有都一起搬過去。
步驟三、續搬View
19
 不是所有都一起搬過去。
步驟三、續搬View
記得修改model
的Namespace
20
步驟三、續搬View(2)
21
 MVC 4 中新增兩個 appSeettings 的屬性值
 MVC 3 相關參考版本
 MVC 4 相關參考版本
步驟四、確認 web.config
22
 如果你的MVC應用程式有使用到一些third-party的Library,如果它可
能使用到舊版的mvc相關參考,請再<configuration>加入如下敘述:
步驟五、確認 web.config (續)
23
 所使用的開發環境
 如何將 MVC3 的專案升級成 MVC4
 ASP.NET MVC 4 RC 新增功能介紹
大綱
24
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
ASP.NET MVC 4 RC 新增功能介紹
25
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
ASP.NET MVC 4 RC 新增功能介紹
26
 同 WCF Data Service ,且支援RESTful介面,但是它更簡化,更簡
單,易於實作。
 只支援HTTP協定,透過 GET/POST 方式提供遠端使用XML或JSON資料格
式進行呼叫與使用。
 更容易讓多種裝置使用:如平板、智慧型裝置、桌上型電腦、其他平台
服務、瀏覽器…
 支援非同步執行特性,有更加之延展性
 支援 ASP.NET MVC 的 Routing 功能
 強行別的HttpRequestMessage與HttpResponseMessage,因此模型繫
結與驗證更加容易。
認識 ASP.NET Web API
27
Web API vs. WCF Service vs. WCF Data
Service
28
ASP.NET 4.5 Web API 架構
29
 使用 Visual Studio 2012 RC 的 Web API 樣板建立
Web API 應用程式。
 使用 jQuery 取得資料。
Demo 1
30
 範例實作、從無到有,建立一個 CRUD 應用程式
Demo 2
31
 使用 SQL Express
 Vistual Studio 2012 RC 會提示資料庫不相容,手動升
級即可。
Demo 2 (續)
32
 使用 SQL Express
 Vistual Studio 2012 RC 會提示資料庫不相容,手動升
級即可。
Demo 2 (續)
33
 使用 SQL Express
 Vistual Studio 2012 RC 會提示資料庫不相容,手動升
級即可。
Demo 2 (續)
34
 使用 SQL Express
 Vistual Studio 2012 RC 會提示資料庫不相容,手動升
級即可。
Demo 2 (續)
35
 當你沒有引用 JSON.NET 物件時,無法掛載應用程式
Self-Host 掛載 Web API (1)
36
 當你沒有引用 JSON.NET 物件時,無法掛載應用程式
Self-Host 掛載 Web API (1)
37
 必須以系統管理員權限執行,否則……
Self-Host 掛載 Web API (2)
38
 將 EDM Model 包裝成 Class Library
 建立 Self-Host Console 專案
 將 MyCusAPIController.cs 檔案加入Self-Host
Console 專案中
Self-Host 掛載 Web API (3)
39
 Seft-Host 基本必須安裝的套件
Self-Host 掛載 Web API (4)
40
 Seft-Host 基本必須安裝的套件
Self-Host 掛載 Web API (4)
41
 撰寫 Self-Host 掛載程式碼
Self-Host 掛載 Web API (5)
42
 Demo…
Self-Host 掛載 Web API (6)
43
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
Enhancements to Default Project
Templates
44
45
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
46
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
47
 判別是否使用手持智慧裝置上網
 使用 檔名 來辨別要顯示的檔案
 從mobile的MVC專案複製相關的
檔案,並將檔案名稱修改為
XXX.mobile.cshtml
 mobile的jQuery相關檔案也必
須複製過來。
Display Modes
48
 屬於 mobile 使用的 jQuery 檔案可透過 NeGet 來安裝
Display Modes (2)
49
 若您還要區分不同的掌上型裝置檢視不同的View時,可以在
Global.asax.cs 加入如下敘述
Display Modes (3)
50
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Azure SDK
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
51
 讓掌上型智慧裝置使用者也可以選擇使用PC版本檢視網頁
 目前Yahoo、MSN 等大型入口網站皆提供此功能。
 透過 User Agent 判斷,因此可以做到特定平台使用特定的
View
 通常與 Display Modes 一起使用。
jQuery Mobile, the View Switcher, and
Browser Overriding
52
 使用方式,在Views/Shared的底下放置
_ViewSwitcher.cshtml檔案,並撰寫如下內容:
jQuery Mobile, the View Switcher, and
Browser Overriding (2)
53
 接著撰寫對應的 ViewSwitcherController
 記得在 _Layout.mobile.cshtml 加上
jQuery Mobile, the View Switcher, and
Browser Overriding (3)
54
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
55
 新增可支援非同步作業傳回值的 Task 類別
 在需要非同步執行的方法中使用 async 關鍵字
 在實際需要等待傳回值的方法前使用 await 關鍵字來等待結果回傳。
新版非同步程式撰寫支援
public async Task<ActionResult> MyCustomers()
{
var myApi = new HttpClient();
myApi.BaseAddress = new Uri("http://localhost:1168");
Task<HttpResponseMessage> response = myApi.GetAsync("api/MyCusAPI/");
IEnumerable<Customers> result = await
response.Result.Content.ReadAsAsync<IEnumerable<Customers>>();
return View(result);
}
56
 新增可支援非同步作業傳回值的 Task 類別
 在需要非同步執行的方法中使用 async 關鍵字
 在實際需要等待傳回值的方法前使用 await 關鍵字來等待結果回傳。
新版非同步程式撰寫支援
public async Task<ActionResult> MyCustomers()
{
var myApi = new HttpClient();
myApi.BaseAddress = new Uri("http://localhost:1168");
Task<HttpResponseMessage> response = myApi.GetAsync("api/MyCusAPI/");
IEnumerable<Customers> result = await
response.Result.Content.ReadAsAsync<IEnumerable<Customers>>();
return View(result);
}
57
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
58
 Code-First 功能再延伸
 資料表Table不需要重新建立,動態加入新欄位
 設定更簡單,更易於使用
Database Migrations
59
 使用 Basic 範本這
樣就不需要手動使用
NuGet加入jQuery &
jQuery UI &
jQuery Validation
等套件。
如何建立Database Migrations的應用程式1
60
 在 Models 資料夾下面建立 Products 類別 (資料表)
如何建立Database Migrations的應用程式2
61
 建立 DbContext 類別
如何建立Database Migrations的應用程式3
62
 編譯後、新增一個 ProductsController
如何建立Database Migrations的應用程式4
63
 編譯後、新增一個 ProductsController
如何建立Database Migrations的應用程式4
64
 編譯後、新增一個 ProductsController
如何建立Database Migrations的應用程式4
65
 預設VS2012 會幫你將資料儲存到
如何建立Database Migrations的應用程式5
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)v11.0;Initial Catalog=aspnet-
Mvc4DatabaseMigrationApp2-20120729210048;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|aspnet-
Mvc4DatabaseMigrationApp2-20120729210048.mdf" />
</connectionStrings>
66
 程式立即
可以執行
 Demo…
如何建立Database Migrations的應用程式6
67
 程式立即
可以執行
 Demo…
如何建立Database Migrations的應用程式6
68
 程式立即
可以執行
 Demo…
如何建立Database Migrations的應用程式6
69
 Data Migration 的主要功能為,在不重建、清除資料的情況下,動態
刪減欄位。
 透過Package Manager Console下Cmdlet指令來完成
 首先須先開啟Migration功能:
70
如何建立Database Migrations的應用程式7
 加入實際的欄位
71
如何建立Database Migrations的應用程式8
 最後一個CmdLet,真的將欄位加入到資料庫中。
72
如何建立Database Migrations的應用程式9
 Demo..
73
如何建立Database Migrations的應用程式10
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
74
 ASP.NET Web API
 Enhancements to Default Project Templates
 Mobile Project Template
 Display Modes
 jQuery Mobile, the View Switcher, and Browser Overriding
 Task Support for Asynchronous Controllers
 Database Migrations
 Empty Project Template
 Add Controller to any project folder
 Bundling and Minification
75
 在HTTP 中傳輸 JavaScript 或 CSS 的時候,它會幫您一
除掉一些不必要 [字元] 與 [空白]
 使用方式..
Bundling and Minification
76
 在HTTP 中傳輸 JavaScript 或 CSS 的時候,它會幫您一
除掉一些不必要 [字元] 與 [空白]
 使用方式..
Bundling and Minification
77
 在HTTP 中傳輸 JavaScript 或 CSS 的時候,它會幫您一
除掉一些不必要 [字元] 與 [空白]
 使用方式..
Bundling and Minification
78
 使用預設的BundleTable的壓縮功能
79
Bundling and Minification (2)
 不使用BundleTable的壓縮功能
80
Bundling and Minification (3)
 Demo…
Bundling and Minification
81
 Blog: http://www.dotblogs.com.tw/gelis/
 MSN: wergelis@msn.com
 Facebook: http://fb.me/gelis.wu
聯絡資訊
82
83
 官網:http://mvc.tw
 臉書:http://fb.me/twmvc
 G+ :http://mvc.tw/+
84
twMVC

More Related Content

What's hot (18)

PDF
twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史
twMVC
 
PDF
AngularJS 開發 ASP.NET MVC -twMVC#9
twMVC
 
PDF
ASP.NET MVC Model 的設計與使用 twMVC#10
twMVC
 
PDF
ASP.NET MVC之實戰架構探討 -twMVC#5
twMVC
 
PDF
輕鬆上手Asp.net web api 2.1-twMVC#14
twMVC
 
PDF
twMVC#22 | 一個微信專案從0到.000的效能調教之路
twMVC
 
PDF
twMVC#28 | visual studio 2017 新功能介紹
twMVC
 
PDF
twMVC#24 | 開發團隊的敏捷之路(未完成)
twMVC
 
PPTX
MVC實戰分享 分頁與排序相關技巧-tw mvc#13
twMVC
 
PPTX
Asp.Net MVC 一教就上手
Study4TW
 
PDF
Vs2013新功能介紹 twMVC#11
twMVC
 
PDF
動手打造 application framework-twMVC#15
twMVC
 
PDF
架構設計入門 twMVC#11
twMVC
 
PDF
開發的效能與效率-twMVC#15
twMVC
 
PDF
專案分層架構 twMVC#18
twMVC
 
PPTX
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
Bruce Chen
 
PDF
twMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC
 
PDF
twMVC#21 | 你所不知道的 Visual Studio
twMVC
 
twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史
twMVC
 
AngularJS 開發 ASP.NET MVC -twMVC#9
twMVC
 
ASP.NET MVC Model 的設計與使用 twMVC#10
twMVC
 
ASP.NET MVC之實戰架構探討 -twMVC#5
twMVC
 
輕鬆上手Asp.net web api 2.1-twMVC#14
twMVC
 
twMVC#22 | 一個微信專案從0到.000的效能調教之路
twMVC
 
twMVC#28 | visual studio 2017 新功能介紹
twMVC
 
twMVC#24 | 開發團隊的敏捷之路(未完成)
twMVC
 
MVC實戰分享 分頁與排序相關技巧-tw mvc#13
twMVC
 
Asp.Net MVC 一教就上手
Study4TW
 
Vs2013新功能介紹 twMVC#11
twMVC
 
動手打造 application framework-twMVC#15
twMVC
 
架構設計入門 twMVC#11
twMVC
 
開發的效能與效率-twMVC#15
twMVC
 
專案分層架構 twMVC#18
twMVC
 
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
Bruce Chen
 
twMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC
 
twMVC#21 | 你所不知道的 Visual Studio
twMVC
 

Similar to ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4 (20)

PDF
20130823微軟雲端平台開發者日
twMVC
 
PDF
Asp.net mvc網站的從無到有
Wade Huang
 
PPTX
Asp.net mvc 基礎
Gelis Wu
 
PPTX
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
Duran Hsieh
 
PPTX
How to ASP.NET MVC4
Daniel Chou
 
PDF
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC
 
PPTX
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
Duran Hsieh
 
PDF
前端工程師與室內裝修師傅的相似之處-twMVC#16
twMVC
 
PDF
twMVC#16 | 前端工程師與室內裝修師傅的相似之處
twMVC
 
PDF
20141212 html5 及微軟跨平台佈局 long
Meng-Ru (Raymond) Tsai
 
PPTX
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
Duran Hsieh
 
PDF
twMVC#02 | ASP.NET MVC 從無到有
twMVC
 
PPTX
Mobile Web(preview version)
Yu Wei Shang
 
PDF
鼎鈞數位行銷App營運實務全攻略
淳甫 鄭
 
PPTX
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Gelis Wu
 
PDF
iPhone/iPad APP Development Class 101
Ryan Chung
 
PPTX
內容管理系統 - Drupal入門
Hen Chen
 
PPTX
ASP NET MVC
leeju lee
 
PPTX
jQuery Mobile
彭其捷 Jack
 
PPTX
Dev camps Windows Store App 市集應用程式最佳實踐
Meng-Ru (Raymond) Tsai
 
20130823微軟雲端平台開發者日
twMVC
 
Asp.net mvc網站的從無到有
Wade Huang
 
Asp.net mvc 基礎
Gelis Wu
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
Duran Hsieh
 
How to ASP.NET MVC4
Daniel Chou
 
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
Duran Hsieh
 
前端工程師與室內裝修師傅的相似之處-twMVC#16
twMVC
 
twMVC#16 | 前端工程師與室內裝修師傅的相似之處
twMVC
 
20141212 html5 及微軟跨平台佈局 long
Meng-Ru (Raymond) Tsai
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
Duran Hsieh
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC
 
Mobile Web(preview version)
Yu Wei Shang
 
鼎鈞數位行銷App營運實務全攻略
淳甫 鄭
 
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Gelis Wu
 
iPhone/iPad APP Development Class 101
Ryan Chung
 
內容管理系統 - Drupal入門
Hen Chen
 
ASP NET MVC
leeju lee
 
jQuery Mobile
彭其捷 Jack
 
Dev camps Windows Store App 市集應用程式最佳實踐
Meng-Ru (Raymond) Tsai
 
Ad

More from twMVC (19)

PDF
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
twMVC
 
PDF
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
twMVC
 
PDF
讓應用程式雲端化 由 Iaa s 邁向 paas 之路-Global Azure Bootcamp 2015 臺北場
twMVC
 
PDF
線上遊戲利用 Dev ops 開源工具 chef, docker, vagrant 在microsoft azure 的佈署實踐-Global Azure...
twMVC
 
PDF
Azure mobileservice
twMVC
 
PDF
一小時可以打造什麼服務Plus twMVC#18
twMVC
 
PDF
SignalR實戰技巧 twmvc#17
twMVC
 
PDF
Angular js twmvc#17
twMVC
 
PDF
TypeScript-twmvc#16
twMVC
 
PDF
樞紐分析表之數據分析與統計-twMV#C14
twMVC
 
PPTX
RWD不是你想的那樣 tw mvc#13
twMVC
 
PPTX
ASP.NET MVC 善用網路資源快速完打造網站
twMVC
 
PDF
如何在實務上使用TDD來開發 twmvc#12
twMVC
 
PDF
Knockout 從零開始,實戰教學 twMVC#10
twMVC
 
PDF
NFC近場通訊-twMVC#9
twMVC
 
PDF
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
twMVC
 
PDF
ASP.NET MVC Web API -twMVC#8
twMVC
 
PDF
超。光速 網站最佳化實戰 -twMVC#8
twMVC
 
PDF
透過測試提升軟體品質 - twMVC#7
twMVC
 
雲端上的資訊安全-Global Azure Bootcamp 2015 臺北場
twMVC
 
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
twMVC
 
讓應用程式雲端化 由 Iaa s 邁向 paas 之路-Global Azure Bootcamp 2015 臺北場
twMVC
 
線上遊戲利用 Dev ops 開源工具 chef, docker, vagrant 在microsoft azure 的佈署實踐-Global Azure...
twMVC
 
Azure mobileservice
twMVC
 
一小時可以打造什麼服務Plus twMVC#18
twMVC
 
SignalR實戰技巧 twmvc#17
twMVC
 
Angular js twmvc#17
twMVC
 
TypeScript-twmvc#16
twMVC
 
樞紐分析表之數據分析與統計-twMV#C14
twMVC
 
RWD不是你想的那樣 tw mvc#13
twMVC
 
ASP.NET MVC 善用網路資源快速完打造網站
twMVC
 
如何在實務上使用TDD來開發 twmvc#12
twMVC
 
Knockout 從零開始,實戰教學 twMVC#10
twMVC
 
NFC近場通訊-twMVC#9
twMVC
 
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
twMVC
 
ASP.NET MVC Web API -twMVC#8
twMVC
 
超。光速 網站最佳化實戰 -twMVC#8
twMVC
 
透過測試提升軟體品質 - twMVC#7
twMVC
 
Ad

ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4