前陣子 在網路上 看到一個網站 介紹 ASP.Net MVC
網站叫做
Learn MVC Project in 7 days
內容真的很棒
我個人獲益非常多
他總共有七篇文章,讓讀者分七天來學習
內容深入淺出
很適合 初學者 學習
範圍也涵蓋的不錯
學完後,應該可以做不少事情
所以 我想在這裡 把他提到的一些重點 摘要在這裡
他裡面 總共有 36個實驗(Lab),也就是36個例子
我想在這裡 主要以這36個實驗為主軸 來介紹他的內容
我摘要的重點原則上是參考原文
但描述的順序和方式可能會和原文不太一樣
我的文章裡,一直都會附上原文的連結
以方便使用者參考原文
=======================================================
Learn MVC Project in 7 days – Day 1
=======================================================
Lab 1 – Demonstrating Controller with a simple hello world
Lab1 – 以一個簡單的「hello world」程式來示範「Controller」)
=======================================================
目的:
(1) 了解 Controller
(2)了解 Action Method
=======================================================
步驟:(詳見原文)
(1)建立一個 MVC 專案
(2)在這個MVC專案裡面,建立一個 控制器叫 TestController
(3)將TestController 裡面的 Index() 方法 刪掉,並加入一個 GetSring方法如下:
(4)在瀏覽器裡打入下列網址:
localhost/Test/GetString
觀察結果如下:
=======================================================
討論:
一、 Controller 名稱的意義:
TestController:Class Name (類別名稱)
Test:Controller Name (控制器名稱)
二、Action Method
(一)意義:Action Method 是一個寫在Controller裡面的 Public Method,它可以接受使用者的要求,並給予使用者一些回應的訊息。
(二)使用者要求 Action Method 的方法:
在網址列打入:網址/Controller Name/Action Method
如上例的: localhost/Test/GetString
請注意,前面寫的是 Controller Name :Test
不是 Class Name :TestController
(三)Action Method 回應使用者一些訊息的方法:
在Action Method的程式 中 寫入 return 指令如下:
(四) 可以回應的訊息種類很多
(1)比如說上例中回應的是字串
寫法如下:
return "Hello World is old now. It’s time for wassup bro ;)";
回應的結果如下:
(2)也可以回應物件,如下例的 return c :
回應的結果如下,它回應的其實是 物件的 ToString() 屬性:
(3) 回應物件的屬性:(本例為覆寫(override)物件的 ToString() Method )
例:
回應結果:
(4) 當然,最常見的是回應 View(),也就是
View 將在下一個 Lab 介紹
三、Controller 裡面的 Method 種類:
(一)Actino Method:一個 Method 如果是 Public ,原則上為 Action Method ,也就是可直接以網址列接受使用者要求的 Method
(二)一般 Method:一個 Method 如果不是 Public,則為 Controller 內部的 一般 Method ,不可接受使用者要求
(三)Public 的一般 Method:如果 Controller 內的一般 Method 想要設成 Public ,可在 Method 上面加上 [NonAction] ,他就不可以接受使用者要求了, 如下例所示:
在這個例子裡,如果我們在網址列打上 localhost/Test/SimpleMethod
則瀏覽器畫面會出現下列錯誤訊息:
網站叫做
Learn MVC Project in 7 days
內容真的很棒
我個人獲益非常多
他總共有七篇文章,讓讀者分七天來學習
內容深入淺出
很適合 初學者 學習
範圍也涵蓋的不錯
學完後,應該可以做不少事情
所以 我想在這裡 把他提到的一些重點 摘要在這裡
他裡面 總共有 36個實驗(Lab),也就是36個例子
我想在這裡 主要以這36個實驗為主軸 來介紹他的內容
我摘要的重點原則上是參考原文
但描述的順序和方式可能會和原文不太一樣
我的文章裡,一直都會附上原文的連結
以方便使用者參考原文
=======================================================
Learn MVC Project in 7 days – Day 1
=======================================================
Lab 1 – Demonstrating Controller with a simple hello world
Lab1 – 以一個簡單的「hello world」程式來示範「Controller」)
=======================================================
目的:
(1) 了解 Controller
(2)了解 Action Method
=======================================================
步驟:(詳見原文)
(1)建立一個 MVC 專案
(2)在這個MVC專案裡面,建立一個 控制器叫 TestController
(3)將TestController 裡面的 Index() 方法 刪掉,並加入一個 GetSring方法如下:
public class TestController : Controller { public string GetString() { return "Hello World is old now. It’s time for wassup bro ;)"; } }
(4)在瀏覽器裡打入下列網址:
localhost/Test/GetString
觀察結果如下:
=======================================================
討論:
一、 Controller 名稱的意義:
TestController:Class Name (類別名稱)
Test:Controller Name (控制器名稱)
二、Action Method
(一)意義:Action Method 是一個寫在Controller裡面的 Public Method,它可以接受使用者的要求,並給予使用者一些回應的訊息。
(二)使用者要求 Action Method 的方法:
在網址列打入:網址/Controller Name/Action Method
如上例的: localhost/Test/GetString
請注意,前面寫的是 Controller Name :Test
不是 Class Name :TestController
(三)Action Method 回應使用者一些訊息的方法:
在Action Method的程式 中 寫入 return 指令如下:
return XXXX
(四) 可以回應的訊息種類很多
(1)比如說上例中回應的是字串
寫法如下:
return "Hello World is old now. It’s time for wassup bro ;)";
回應的結果如下:
(2)也可以回應物件,如下例的 return c :
namespace WebApplication1.Controllers { public class Customer { public string CustomerName { get; set; } public string Address { get; set; } } public class TestController : Controller { public Customer GetCustomer() { Customer c = new Customer(); c.CustomerName = "Customer 1"; c.Address = "Address1"; return c; } } }
回應的結果如下,它回應的其實是 物件的 ToString() 屬性:
(3) 回應物件的屬性:(本例為覆寫(override)物件的 ToString() Method )
例:
public override string ToString() { return this.CustomerName+"|"+this.Address; }
回應結果:
(4) 當然,最常見的是回應 View(),也就是
return View()
View 將在下一個 Lab 介紹
三、Controller 裡面的 Method 種類:
(一)Actino Method:一個 Method 如果是 Public ,原則上為 Action Method ,也就是可直接以網址列接受使用者要求的 Method
(二)一般 Method:一個 Method 如果不是 Public,則為 Controller 內部的 一般 Method ,不可接受使用者要求
(三)Public 的一般 Method:如果 Controller 內的一般 Method 想要設成 Public ,可在 Method 上面加上 [NonAction] ,他就不可以接受使用者要求了, 如下例所示:
[NonAction] public string SimpleMethod() { return "Hi, I am not action method"; }
在這個例子裡,如果我們在網址列打上 localhost/Test/SimpleMethod
則瀏覽器畫面會出現下列錯誤訊息: