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

PDF
ASP.NET MVC Model 的設計與使用 twMVC#10
PDF
ASP.NET MVC Code Templates實戰開發 -twMVC#4
PDF
AngularJS 開發 ASP.NET MVC -twMVC#9
PDF
ASP.NET MVC之實戰架構探討 -twMVC#5
PDF
架構行動式網站(使用 ASP.NET MVC 4.0 -twMVC#6
PPTX
twMVC#01 | ASP.NET MVC 的第一次親密接觸
PDF
ASP.NET MVC 新增、查詢、修改、刪除 基本功實戰 -twMVC#3
PDF
Asp.net mvc 從無到有 -twMVC#2
ASP.NET MVC Model 的設計與使用 twMVC#10
ASP.NET MVC Code Templates實戰開發 -twMVC#4
AngularJS 開發 ASP.NET MVC -twMVC#9
ASP.NET MVC之實戰架構探討 -twMVC#5
架構行動式網站(使用 ASP.NET MVC 4.0 -twMVC#6
twMVC#01 | ASP.NET MVC 的第一次親密接觸
ASP.NET MVC 新增、查詢、修改、刪除 基本功實戰 -twMVC#3
Asp.net mvc 從無到有 -twMVC#2

What's hot (18)

PDF
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
PDF
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
PDF
架構設計入門 twMVC#11
PDF
如何在實務上使用TDD來開發 twmvc#12
PDF
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
PPTX
輕鬆上手ASP.NET Web API 2.1.2
PDF
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
PDF
twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史
PPTX
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
PDF
twMVC#20 | ASP.NET MVC View 開發技巧小錦囊
PPTX
ASP.NET MVC 善用網路資源快速完打造網站
PPTX
MVC實戰分享 分頁與排序相關技巧-tw mvc#13
PDF
Vs2013新功能介紹 twMVC#11
PDF
SignalR實戰技巧 twmvc#17
PDF
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
PDF
開發的效能與效率-twMVC#15
PDF
一小時可以打造什麼服務Plus twMVC#18
PPTX
Asp.Net MVC 一教就上手
使用 ASP.NET MVC 開發SPA網站-微軟實戰課程日
twMVC#05 |開發與移轉 ASP.NET MVC 4.0 應用程式到 Windows Azure Platform
架構設計入門 twMVC#11
如何在實務上使用TDD來開發 twmvc#12
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
輕鬆上手ASP.NET Web API 2.1.2
ASP.NET MVC 4 新功能介紹(快速上手) -twMVC#4
twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
twMVC#20 | ASP.NET MVC View 開發技巧小錦囊
ASP.NET MVC 善用網路資源快速完打造網站
MVC實戰分享 分頁與排序相關技巧-tw mvc#13
Vs2013新功能介紹 twMVC#11
SignalR實戰技巧 twmvc#17
與 Asp.net mvc 的第一次親密接觸 - twMVC#1
開發的效能與效率-twMVC#15
一小時可以打造什麼服務Plus twMVC#18
Asp.Net MVC 一教就上手
Ad

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

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

More from twMVC (20)

PDF
twMVC#51 以平台工程重新思考系統設計 - 以 Batch System 為例封面
PDF
twMVC#51-GitHub Copilot 徹底改變開發模式,探索 AI 驅動的智慧程式碼協作
PDF
twMVC#50 微服務上線後的救贖
PDF
twMVC 47_Elastic APM 的兩三事
PDF
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
PDF
.NET 7 家族新成員: Microsoft Orleans v7
PDF
twMVC#46 一探 C# 11 與 .NET 7 的神奇
PDF
twMVC#44 如何測試與保護你的 web application with playwright
PDF
twMVC#44 讓我們用 k6 來進行壓測吧
PDF
twMVC#43 Visual Studio 2022 新功能拆解
PDF
twMVC#43 YARP
PDF
twMVC#43 C#10 新功能介紹
PDF
twMVC#42 Azure DevOps Service Pipeline設計與非正常應用
PDF
twMVC#42 Azure IoT Hub for Smart Factory
PDF
twMVC#42 Windows容器導入由0到1
PDF
twMVC#42 讓我們用一種方式來開發吧
PDF
twMVC#41 hololens2 MR
PPTX
twMVC#41 The journey of source generator
PDF
twMVC#38 How we migrate tfs to git(using azure dev ops)
PDF
twMVC#36C#的美麗與哀愁
twMVC#51 以平台工程重新思考系統設計 - 以 Batch System 為例封面
twMVC#51-GitHub Copilot 徹底改變開發模式,探索 AI 驅動的智慧程式碼協作
twMVC#50 微服務上線後的救贖
twMVC 47_Elastic APM 的兩三事
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
.NET 7 家族新成員: Microsoft Orleans v7
twMVC#46 一探 C# 11 與 .NET 7 的神奇
twMVC#44 如何測試與保護你的 web application with playwright
twMVC#44 讓我們用 k6 來進行壓測吧
twMVC#43 Visual Studio 2022 新功能拆解
twMVC#43 YARP
twMVC#43 C#10 新功能介紹
twMVC#42 Azure DevOps Service Pipeline設計與非正常應用
twMVC#42 Azure IoT Hub for Smart Factory
twMVC#42 Windows容器導入由0到1
twMVC#42 讓我們用一種方式來開發吧
twMVC#41 hololens2 MR
twMVC#41 The journey of source generator
twMVC#38 How we migrate tfs to git(using azure dev ops)
twMVC#36C#的美麗與哀愁

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