Presentation is loading. Please wait.

Presentation is loading. Please wait.

利用 ASP.NET MVC 提升您的 Web 應用程式

Similar presentations


Presentation on theme: "利用 ASP.NET MVC 提升您的 Web 應用程式"— Presentation transcript:

1 利用 ASP.NET MVC 提升您的 Web 應用程式
Tech·Days 4/6/2019 6:07 AM 利用 ASP.NET MVC 提升您的 Web 應用程式 董大偉 台灣微軟MSDN講座講師 TechED 2007, 2008講師 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

2 Develop Web Application with ASP.NET MVC 本場次大綱
4/6/2019 6:07 AM Develop Web Application with ASP.NET MVC 本場次大綱 從5W1H看ASP.NET MVC 什麼是MVC? 透過這樣的架構開發的應用程式有何優點? ASP.NET MVC Framework又是什麼? 和ASP.NET WebForm技術不同嗎? 如何利用ASP.NET MVC來開發MVC應用程式? 如何撰寫出較好的Web應用程式? © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

3 解決方案總是從問題而來 - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 解決方案總是從問題而來 - Develop Web Application with ASP.NET MVC ASP.NET MVC的 5W 1H WHAT - 什麼是ASP.NET MVC? WHEN - 何時需要使用ASP.NET MVC? WHY 為何要使用ASP.NET MVC? WHO 誰適合來使用ASP.NET MVC? WHERE - 在些場合中需要使用ASP.NET MVC? HOW 如何開發ASP.NET MVC 應用程式? © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

4 什麼是MVC? - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 什麼是MVC? - Develop Web Application with ASP.NET MVC MVC… 是一種設計模式。 MVC這個Pattern要求我們在開發程式的時候,把我們要透過程式碼達成的一個功能,在設計時切割成Model、View、Controller這三個部分(的程式碼)。 每一個部分可以獨立互不干涉,但執行時可以互相合作,已達成功能,開發時亦可由不同的開發人員進行開發、同時也便於Unit Test。 目的: 降低程式碼之間的相依性、提高重用性 便於多人開發、單元測試、降低開發成本 結構清晰、利於後續維護 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

5 什麼是MVC? - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 什麼是MVC? - Develop Web Application with ASP.NET MVC Controller Model View © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

6 什麼是MVC? - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 什麼是MVC? - Develop Web Application with ASP.NET MVC ASP.NET MVC 微軟平台上新提供的MVC開發架構,透過這組類別庫,開發人員可以MVC架構來建立Web應用程式。這組MVC架構可以同時提高程式的延展性與彈性,降低.aspx頁面與後端資料庫、商業邏輯之間的相依性,達成大型專案的快速開發、高重用性、容易調整與維護的目的。 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

7 什麼是ASP.NET MVC Framework - Develop Web Application with ASP.NET MVC
架構在ASP.NET技術上的一組Framework。 讓開發人員得以輕鬆建立出MVC架構的應用程式。 提供建構MVC應用程式所需的基礎類別、以及相關工具(Helper) 。 概念上與ASP.NET WebForm並不相同,不支援事件驅動、Web Controls、或是Postback…等機制。 支援了一些ASP.NET過去相當優秀的設計,例如Master-Page、MemberShip、UserControl等機制。 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

8 ASP.NET MVC Framework View Controller Model
負責與UI顯示有關的部分,在這個部分當中的程式碼,只應該與UI(或UI上的操作)有關,不應該有任何與商業邏輯、後端資料庫有關的任何程式碼,對於Web應用程式來說,就是Render出HTML與前端JavaScript操作的部分。 View 這個部分的程式碼可以是部分的商業邏輯、可以是應用程式當中所需要的運算規則或演算法、Controller中的程式碼可以依照實際的需求(功能)決定調用(或存取)哪一個Model、可以動態的決定用哪一個View來呈現運算後的結果,Controller同時也負責回應前端View所產生的事件(或需求)。 Controller 一般來說就是Data Model,同時負責了實際資料庫存取的部分,這部分的程式碼負責把後端資料庫給封裝起來,讓Controller或View可以完全不(需要)知道(或不在乎)後端資料庫的長相究竟為何,只需要透過Model即可正確的存取後端資料庫。 Model 降低相依性, 不能假設View知道Controller, 或Contoller知道View, 僅依照ViewData交換資料, Contoller可以決定用哪一 個View或Model…

9 ASP.NET MVC Framework Models Views Controllers Bmi HR Data Model
Post .aspx Data Model Member HR Bmi Get LinqToSql(.dbml) .aspx 降低相依性, 不能假設View知道Controller, 或Contoller知道View, 僅依照ViewData交換資料, Contoller可以決定用哪一 個View或Model… .aspx ViewData ADO.NET Entity Framework (.edmx)

10 MVC三合一應用程式  這種程式我們稱為:MVC三合一寫法 『一氣呵成』、從前端殺到後端的『直搗狂龍式』寫法ClassicBmiWebAP
4/6/2019 6:07 AM MVC三合一應用程式  Protected Sub Button_GetBMI_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_GetBMI.Click Dim BMI As Single Dim height, weight As Single '從UI取得身高體重 height = Me.TextBox_Height.Text weight = Me.TextBox_Weight.Text '計算BMI(主要邏輯運算) BMI = weight / (height / 100) ^ 2 '顯示BMI Me.Label_Result.Text = "計算結果 BMI=" & BMI ‘將資料儲存到資料庫 SqlDataSource1.InsertCommand = "Insert into BmiData (Height,Weight,BMI) values SqlDataSource1.InsertParameters.Add("Height", height) SqlDataSource1.InsertParameters.Add("Weight", weight) SqlDataSource1.InsertParameters.Add("BMI", BMI) '顯示訊息 If SqlDataSource1.Insert() > 0 Then ClientScript.RegisterStartupScript(Me.GetType, "", "alert('儲存成功');", True) Else ClientScript.RegisterStartupScript(Me.GetType, "", "alert('儲存失敗');", True) End If Dim query As System.Data.Services.Client.DataServiceQuery(Of crNorthWindModel.客戶) = DirectCast(result.AsyncState, System.Data.Services.Client.DataServiceQuery(Of crNorthWindModel.客 戶)) '將抓取到的資料填入DataGrid DataGrid1.ItemsSource = query.EndExecute(result).ToList() '顯示訊息 Windows.Browser.HtmlPage.Window.Alert("OK") 這種程式我們稱為:MVC三合一寫法 『一氣呵成』、從前端殺到後端的『直搗狂龍式』寫法ClassicBmiWebAP © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

11 MVC三合一的問題 - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM MVC三合一的問題 - Develop Web Application with ASP.NET MVC 在同一個事件當中, 又處理UI, 又處理邏輯, 又存取DB 前端.aspx頁面與後端資料庫密不可分 前端.aspx頁面與後端商業邏輯程式碼難分難捨 即使具有DataAccess Class也難免保證開發人員遵循(*) 牽一髮而動全身 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12 MVC三合一的問題 - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM MVC三合一的問題 - Develop Web Application with ASP.NET MVC 應該如何改進? 將Business Logic獨立出來 將資料庫存取部分獨立出來 結果: 程式碼相依性降低、可重用性提高 程式碼變多? 效能會提升嗎? © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

13 DEMO:調整MVC三合一程式,使之 將Business Logic獨立出來 將資料庫存取部分獨立出來 程式碼相依性降低、可重用性提高
4/6/2019 6:07 AM DEMO:調整MVC三合一程式,使之 將Business Logic獨立出來 將資料庫存取部分獨立出來 程式碼相依性降低、可重用性提高 介紹現成 做一個Login視窗的Demo SimpleLogin D:\MyDoc\0T.教育訓練\Microsoft MSDN講座-ASP.NET MVC\ClassicBmiWebAP2 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

14 MVC的目的 - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM MVC的目的 - Develop Web Application with ASP.NET MVC 避免在同一段程式碼當中, 又處理UI, 又處理邏輯, 又存取DB… 降低程式之間的相依性、提高重用性 讓程式碼結構更加清晰 不同的模塊可由擅長該技術的專職開發人員進行開發,降低開發成本,提高產能 便於Unit Test、使TDD成為可能 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

15 DEMO:建構第一個MVC應用程式 建立ASP.NET MVC Web Application
4/6/2019 6:07 AM DEMO:建構第一個MVC應用程式 建立ASP.NET MVC Web Application 建立BMI Controller與Index Action 建立ViewPage(含欄位) 撰寫BMI Class 在Controller中接收參數 調用BMI Class, 輸出結果 介紹現成 做一個Login視窗的Demo SimpleLogin D:\MyDoc\0T.教育訓練\Microsoft MSDN講座-ASP.NET MVC\BmiMvcApp © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

16 ASP.NET MVC Framework MVC希望在程式面達成的效果 View與Business Login、Model之間徹底脫鉤絕緣
透過Controller轉派 資料來源:

17 認識Controller - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Controller - Develop Web Application with ASP.NET MVC 負責接收Browser端透過HTTP GET/POST傳來的Request Url可決定該Request由哪一個Controller、哪一個Action來負責 Controller當中可以進行各種運算、亦可能包含商業邏輯、各種演算法…等程式碼。 如果有需要,可調用Model來進行後端資料庫存取的動作。 最後決定由哪一個View來處理結果的呈現。 同時將View需要呈現的資料透過ViewData傳遞給View。 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

18 關於URL Routing - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 關於URL Routing - Develop Web Application with ASP.NET MVC URL Routing機制負責將Browser傳來的Request對應到Controller與Action。 在這種設計架構下,我們可以用同一個『頁面』負責同一種『工作』 。 可降低頁面的數量,每一個頁面(View)清楚的負責一種『功能』,不需要撰寫許多頁面負責同樣的功能(例如不同資料表的顯示)。 URL直接對應到『功能』 ,在邏輯上更加直覺。 Web Site Controller Action © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

19 認識Controller - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Controller - Develop Web Application with ASP.NET MVC Controller中程式碼的撰寫方式, 同名處理不同的Verbs ' HTTP GET: /Bmi/Index Function Index() As ActionResult (…略…) Return View() End Function ' HTTP POST: /Bmi/Index <AcceptVerbs(HttpVerbs.Post)> _ Function Index(ByVal TxbHeight As Single, ByVal TxbWeight As Single) As ActionResult 使用預設的View(也就是BMI/Index.aspx)來回應 從View欄位接收參數 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

20 關於URL Routing - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 關於URL Routing - Develop Web Application with ASP.NET MVC 例如,底下的URL可以用來顯示編號為17的產品: 請注意Global.asax檔案(設定Url Route, 底下為預設) Web Site Controller Action ID routes.MapRoute( _ "Default", _ "{controller}/{action}/{id}", _ New With {.controller = "Home", .action = "Index", .id = ""} _ ) © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

21 認識Controller - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Controller - Develop Web Application with ASP.NET MVC Controller中程式碼的撰寫方式 各種ActionResult ViewResult, EmptyResult, RedirectResult, JavaScriptResult, ContentResult, FileContentResult, FilePathresult, FileStreamResult, … ' GET: /product/ShowDetails Function ShowDetails(ID As Integer) As ActionResult '取得ID '透過Data Model抓取資料庫中的內容 ‘將資料傳遞給ViewPage Return View() End Function © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

22 Controller如何與View溝通? - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM Controller如何與View溝通? - Develop Web Application with ASP.NET MVC 抓取ViewPage上的欄位值 ' POST: /Bmi/Index <AcceptVerbs(HttpVerbs.Post)> _ Function Index(ByVal TxbHeight As Single, ByVal TxbWeight As Single) As ActionResult (…略…) '使用預設的VIew Return View() End Function © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

23 Controller如何與View溝通? - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM Controller如何與View溝通? - Develop Web Application with ASP.NET MVC 透過ViewData將資料傳給ViewPage ' POST: /Bmi/Index <AcceptVerbs(HttpVerbs.Post)> _ Function Index(ByVal TxbHeight As Single, ByVal TxbWeight As Single) As ActionResult Dim BMI As Single Dim MyBMI As New BMI '使用BMI類別進行運算 MyBMI.Height = TxbHeight MyBMI.Weight = TxbWeight BMI = MyBMI.GetBMI() '將執行結果傳遞給View ViewData("result") = BMI '使用預設的VIew Return View() End Function © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

24 Controller如何與View溝通? - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM Controller如何與View溝通? - Develop Web Application with ASP.NET MVC 透過ViewData將資料傳給ViewPage ' POST: /Bmi/Index <AcceptVerbs(HttpVerbs.Post)> _ Function Index(ByVal TxbHeight As Single, ByVal TxbWeight As Single) As ActionResult (…略…) Return View(Model) End Function 可傳遞任何的物件給ViewPage © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

25 認識Controller - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Controller Develop Web Application with ASP.NET MVC 建立在Controllers資料夾中 繼承自System.Web.Mvc.Controller 負責回應Browser的各種Request 如何從ViewPage接收參數? 透過ID、透過ViewPage上的欄位 如何傳遞參數給ViewPage? 透過ViewData、透過ViewResult的Model參數 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

26 認識ViewPage - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識ViewPage - Develop Web Application with ASP.NET MVC 負責展示層UI的顯示。 與商業邏輯運算、後端資料庫均無關。 在這個部分當中可以有程式碼,但程式碼只應該與UI(或UI上的操作)有關,不應該有任何與商業邏輯、後端資料庫有關的任何程式碼,對於Web應用程式來說,主要就是Render出HTML與前端JavaScript操作的部分。 繼承於System.Web.Mvc.ViewPage 。 可透過ViewData取得來自Controller的資料。 可透過Model取得來自Controller的複雜資料。 具有HtmlHelper可使用,便於開發。 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

27 認識ViewPage - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識ViewPage - Develop Web Application with ASP.NET MVC 與過去ASP類似的程式撰寫方式 可透過HTML Helper協助產生需要的Html Element 不支援Web Controls 可透過ViewData抓取到Controller傳遞過來的資料 <% Using Html.BeginForm() %> <br/>身高:<% =Html.TextBox("TxbHeight") %> <br/>體重:<% =Html.TextBox("TxbWeight") %> <input type="submit" value="Calculate" /> <% End Using%> <% If ViewData("result") IsNot Nothing Then Response.Write("BMI:" & ViewData("result")) %> © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

28 認識ViewPage - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識ViewPage - Develop Web Application with ASP.NET MVC Html Helper支援… Html.ActionLink(linkText,ActionName,ControllerName) Html.TextBox(name) Html.TextArea(name) Html.RadioButton(name,value) Html.BeginForm() Html.Encode(value) © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

29 DEMO:將MVC應用程式加入資料庫存取
4/6/2019 6:07 AM DEMO:將MVC應用程式加入資料庫存取 使用先前的BmiMvcApp 加入資料庫 調整ViewPage(新增欄位) 加入LinqToSql Data Model(封裝資料庫) 介紹現成 做一個Login視窗的Demo SimpleLogin D:\MyDoc\0T.教育訓練\Microsoft MSDN講座-ASP.NET MVC\BmiMvcAppWithDB © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

30 認識Model - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Model Develop Web Application with ASP.NET MVC 負責了實際資料庫存取的部分。 這部分的程式碼負責把後端資料庫給封裝起來,讓Controller或View可以完全不(需要)知道(或不在乎)後端資料庫的長相究竟為何,只需要透過Model即可正確的存取後端資料庫。 有效的隔離展示層程式碼與後端資料庫 可透過Bind修飾字將ViewPage上的資料繫結到Controller的參數,以便於儲存到DB Contoller中可透過ViewResult的Model參數將從DB取得的資料以物件型態傳遞給ViewPage。(請注意ViewPage須設定泛型型別) © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

31 認識Model - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Model Develop Web Application with ASP.NET MVC Contoller中可透過ViewResult的Model參數將從DB取得的資料,以物件型態傳遞給ViewPage。(請注意ViewPage須設定泛型型別) Dim db As New AddressBookDBDataContext ' ' HTTP GET: /AddressBook/ Function Index() As ActionResult Return View(db.AddressBook.ToList) End Function © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

32 認識Model - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Model Develop Web Application with ASP.NET MVC 承接的ViewPage,需要設定泛型型別,以便於取得資料 Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of AddressBookMvcApp.AddressBook))" %> <% For Each item In Model%> <tr> <td> <%=Html.ActionLink("Edit", "Edit", New With {.id = item.uid})%> | <%=Html.ActionLink("Details", "Details", New With {.id = item.uid})%> </td> <td> <%= Html.Encode(item.uid) %> </td> <td> <%= Html.Encode(item.cName) %> </td> <td> <%= Html.Encode(item.cTel) %> </td> <td> <%= Html.Encode(item.cMemo) %> </td> </tr> <% Next%> </table> </asp:Content> © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

33 認識Model - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 認識Model Develop Web Application with ASP.NET MVC 儲存(回寫)資料時,亦可透過Bind修飾字將ViewPage上的資料繫結到Controller的參數,以便於儲存到DB。 'Post: /Home/Create <AcceptVerbs(HttpVerbs.Post)> _ Function Create(<Bind(exclude:="uid")> ByVal TableData As AddressBookMvcApp.AddressBook) As ActionResult db.AddressBook.InsertOnSubmit(TableData) db.SubmitChanges() Return RedirectToAction("Index") End Function © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

34 DEMO:建立資料庫處理MVC應用程式 AddressBookMvcApp 加入資料庫AddressBookDB.mdf
4/6/2019 6:07 AM DEMO:建立資料庫處理MVC應用程式 AddressBookMvcApp 加入資料庫AddressBookDB.mdf 建立Data Model 建立並規劃AddressBook Controller Index – 顯示所有紀錄 Create– 建立資料 建立相對應的ViewPage(可用Wizard) 撰寫Controller程式碼 介紹現成 做一個Login視窗的Demo SimpleLogin D:\MyDoc\0T.教育訓練\Microsoft MSDN講座-ASP.NET MVC\AddressBookMvcApp © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

35 關於Unit Test - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 關於Unit Test - Develop Web Application with ASP.NET MVC 由於View與Business Logic已經切割開來, 有助於Unit Test的進行。 <TestMethod()> Public Sub TestMethod1() Dim theBmiController As New BmiController Dim theViewResult As ViewResult = theBmiController.Index(170, 70) Assert.AreEqual(" ", theViewResult.ViewData("result").ToString) End Sub D:\MyDoc\0T.教育訓練\Microsoft MSDN講座-ASP.NET MVC\BmiMvcApp © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

36 解決方案總是從問題而來 - Develop Web Application with ASP.NET MVC
4/6/2019 6:07 AM 解決方案總是從問題而來 - Develop Web Application with ASP.NET MVC ASP.NET MVC的 5W 1H WHAT - 什麼是ASP.NET MVC? WHEN - 何時需要使用ASP.NET MVC? WHY 為何要使用ASP.NET MVC? WHO 誰適合來使用ASP.NET MVC? WHERE - 在些場合中需要使用ASP.NET MVC? HOW 如何開發ASP.NET MVC 應用程式? © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

37 Summary MVC架構有助於降低程式碼相依性,提高重用性 讓系統便於測試、可同時多人開發、降低開發成本
4/6/2019 6:07 AM Summary MVC架構有助於降低程式碼相依性,提高重用性 讓系統便於測試、可同時多人開發、降低開發成本 程式碼長度(數量)並不會減少、效能不一定會提升 適合大型(或超大型)的應用系統 MVC著重於架構、並非開發的速度、但當系統規模大到一定程度之後,MVC架構有助於提高產能 將適合的技術用在適當的場合… © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

38 ASP.NET MVC相關資源 筆者 BLOG http://blog.studyhost.com/
4/6/2019 6:07 AM ASP.NET MVC相關資源 筆者 BLOG 筆者 RUN!PC ASP.NET 3.5 SP1-4.0專欄 ASP.NET網站MVC篇: © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

39 4/6/2019 6:07 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "利用 ASP.NET MVC 提升您的 Web 應用程式"

Similar presentations


Ads by Google