<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Learn MVC Project in 7 days – Day 1  –  Lab 1 – Demonstrating Controller with a simple hello world</title>
        <description>前陣子 在網路上 看到一個網站 介紹 ASP.Net MVC

網站叫做

[url=https://www.codeproject.com/articles/866143/learn-mvc-step-by-step-in-days-day]Learn MVC Project in 7 days[/url]

內容真的很棒
我個人獲益非常多
他總共有七篇文章，讓讀者分七天來學習

內容深入淺出
很適合 初學者 學習
範圍也涵蓋的不錯
學完後，應該可以做不少事情
所以 我想在這裡 把他提到的一些重點 摘要在這裡

他裡面 總共有  36個實驗(Lab)，也就是36個例子
我想在這裡 主要以這36個實驗為主軸 來介紹他的內容

我摘要的重點原則上是參考原文
但描述的順序和方式可能會和原文不太一樣

我的文章裡，一直都會附上原文的連結
以方便使用者參考原文

=======================================================


[url=https://www.codeproject.com/articles/866143/learn-mvc-step-by-step-in-days-day]Learn MVC Project in 7 days – Day 1[/url]


=======================================================

[url=https://www.codeproject.com/articles/866143/learn-mvc-project-in-days-day#Lab1%E2%80%93DemonstratingControllerwithasimpleMVChelloworld]Lab 1 – Demonstrating Controller with a simple hello world[/url]
Lab1 – 以一個簡單的「hello world」程式來示範「Controller」)

=======================================================

目的：
(1) 了解 Controller
(2)了解 Action Method

=======================================================

步驟：(詳見原文)

(1)建立一個 MVC 專案
(2)在這個MVC專案裡面，建立一個 控制器叫 TestController
(3)將TestController 裡面的 Index() 方法 刪掉，並加入一個 GetSring方法如下：
   [code]
public class TestController : Controller
{
    public string GetString()
    {
        return &quot;Hello World is old now. It’s time for wassup bro ;)&quot;;
    }
}
[/code]

(4)在瀏覽器裡打入下列網址：
  localhost/Test/GetString

觀察結果如下：

[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.24.png[/img]

=======================================================

討論：

一、 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 指令如下：
 
[code]
 return XXXX
[/code]
  

 (四) 可以回應的訊息種類很多

    (1)比如說上例中回應的是字串
    
     寫法如下：

      return &quot;Hello World is old now. It’s time for wassup bro ;)&quot;;

     回應的結果如下：

       [img]https://www.codeproject.com/KB/aspnet/866143/lab_1.24.png[/img]

  (2)也可以回應物件，如下例的 return c ：


[code]
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 = &quot;Customer 1&quot;;
            c.Address = &quot;Address1&quot;;
            return c;
        }
    }
}    
[/code]

回應的結果如下，它回應的其實是 物件的 ToString() 屬性：

[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.25.png[/img]


   (3) 回應物件的屬性：(本例為覆寫(override)物件的 ToString()  Method )
  
 例：
[code]
public override string ToString()
{
     return this.CustomerName+&quot;|&quot;+this.Address;
}
[/code]

回應結果：

[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.26.png[/img]

(4) 當然，最常見的是回應 View()，也就是

[code]
return View()
[/code]

View 將在下一個 Lab 介紹




三、Controller 裡面的 Method 種類：

(一)Actino Method：一個 Method 如果是 Public ，原則上為 Action Method ，也就是可直接以網址列接受使用者要求的 Method
(二)一般 Method：一個 Method 如果不是 Public，則為 Controller 內部的 一般 Method ，不可接受使用者要求
(三)Public 的一般 Method：如果 Controller 內的一般 Method 想要設成 Public ，可在 Method 上面加上  [NonAction] ，他就不可以接受使用者要求了， 如下例所示：

[code]
[NonAction]
public string SimpleMethod()
{
    return &quot;Hi, I am not action method&quot;;
}
[/code]

在這個例子裡，如果我們在網址列打上 localhost/Test/SimpleMethod
則瀏覽器畫面會出現下列錯誤訊息：

[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.27.png[/img]</description>
        <link>http://mepopedia.com/forum/read.php?37,83876,83876#msg-83876</link>
        <lastBuildDate>Thu, 25 Jun 2026 00:22:06 +0800</lastBuildDate>
        <generator>Phorum 5.2.7</generator>
        <item>
            <guid>http://mepopedia.com/forum/read.php?37,83876,83876#msg-83876</guid>
            <title>Learn MVC Project in 7 days – Day 1  –  Lab 1 – Demonstrating Controller with a simple hello world</title>
            <link>http://mepopedia.com/forum/read.php?37,83876,83876#msg-83876</link>
            <description><![CDATA[前陣子 在網路上 看到一個網站 介紹 ASP.Net MVC<br />
<br />
網站叫做<br />
<br />
[url=https://www.codeproject.com/articles/866143/learn-mvc-step-by-step-in-days-day]Learn MVC Project in 7 days[/url]<br />
<br />
內容真的很棒<br />
我個人獲益非常多<br />
他總共有七篇文章，讓讀者分七天來學習<br />
<br />
內容深入淺出<br />
很適合 初學者 學習<br />
範圍也涵蓋的不錯<br />
學完後，應該可以做不少事情<br />
所以 我想在這裡 把他提到的一些重點 摘要在這裡<br />
<br />
他裡面 總共有  36個實驗(Lab)，也就是36個例子<br />
我想在這裡 主要以這36個實驗為主軸 來介紹他的內容<br />
<br />
我摘要的重點原則上是參考原文<br />
但描述的順序和方式可能會和原文不太一樣<br />
<br />
我的文章裡，一直都會附上原文的連結<br />
以方便使用者參考原文<br />
<br />
=======================================================<br />
<br />
<br />
[url=https://www.codeproject.com/articles/866143/learn-mvc-step-by-step-in-days-day]Learn MVC Project in 7 days – Day 1[/url]<br />
<br />
<br />
=======================================================<br />
<br />
[url=https://www.codeproject.com/articles/866143/learn-mvc-project-in-days-day#Lab1%E2%80%93DemonstratingControllerwithasimpleMVChelloworld]Lab 1 – Demonstrating Controller with a simple hello world[/url]<br />
Lab1 – 以一個簡單的「hello world」程式來示範「Controller」)<br />
<br />
=======================================================<br />
<br />
目的：<br />
(1) 了解 Controller<br />
(2)了解 Action Method<br />
<br />
=======================================================<br />
<br />
步驟：(詳見原文)<br />
<br />
(1)建立一個 MVC 專案<br />
(2)在這個MVC專案裡面，建立一個 控制器叫 TestController<br />
(3)將TestController 裡面的 Index() 方法 刪掉，並加入一個 GetSring方法如下：<br />
   [code]<br />
public class TestController : Controller<br />
{<br />
    public string GetString()<br />
    {<br />
        return "Hello World is old now. It’s time for wassup bro ;)";<br />
    }<br />
}<br />
[/code]<br />
<br />
(4)在瀏覽器裡打入下列網址：<br />
  localhost/Test/GetString<br />
<br />
觀察結果如下：<br />
<br />
[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.24.png[/img]<br />
<br />
=======================================================<br />
<br />
討論：<br />
<br />
一、 Controller 名稱的意義：<br />
<br />
   TestController：Class Name (類別名稱)<br />
   Test：Controller Name (控制器名稱)<br />
<br />
<br />
二、Action Method<br />
<br />
  (一)意義：Action Method 是一個寫在Controller裡面的 Public Method，它可以接受使用者的要求，並給予使用者一些回應的訊息。<br />
<br />
  (二)使用者要求 Action Method 的方法：<br />
<br />
    在網址列打入：網址/Controller Name/Action Method<br />
    如上例的：   localhost/Test/GetString<br />
<br />
    請注意，前面寫的是 Controller Name ：Test<br />
   不是 Class Name ：TestController<br />
<br />
  (三)Action Method 回應使用者一些訊息的方法：<br />
  <br />
    在Action Method的程式 中 寫入 return 指令如下：<br />
 <br />
[code]<br />
 return XXXX<br />
[/code]<br />
  <br />
<br />
 (四) 可以回應的訊息種類很多<br />
<br />
    (1)比如說上例中回應的是字串<br />
    <br />
     寫法如下：<br />
<br />
      return "Hello World is old now. It’s time for wassup bro ;)";<br />
<br />
     回應的結果如下：<br />
<br />
       [img]https://www.codeproject.com/KB/aspnet/866143/lab_1.24.png[/img]<br />
<br />
  (2)也可以回應物件，如下例的 return c ：<br />
<br />
<br />
[code]<br />
namespace WebApplication1.Controllers<br />
{<br />
    public class Customer<br />
    {<br />
        public string CustomerName { get; set; }<br />
        public string Address { get; set; }<br />
    }<br />
    public class TestController : Controller<br />
    {<br />
        public Customer GetCustomer()<br />
        {<br />
            Customer c = new Customer();<br />
            c.CustomerName = "Customer 1";<br />
            c.Address = "Address1";<br />
            return c;<br />
        }<br />
    }<br />
}    <br />
[/code]<br />
<br />
回應的結果如下，它回應的其實是 物件的 ToString() 屬性：<br />
<br />
[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.25.png[/img]<br />
<br />
<br />
   (3) 回應物件的屬性：(本例為覆寫(override)物件的 ToString()  Method )<br />
  <br />
 例：<br />
[code]<br />
public override string ToString()<br />
{<br />
     return this.CustomerName+"|"+this.Address;<br />
}<br />
[/code]<br />
<br />
回應結果：<br />
<br />
[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.26.png[/img]<br />
<br />
(4) 當然，最常見的是回應 View()，也就是<br />
<br />
[code]<br />
return View()<br />
[/code]<br />
<br />
View 將在下一個 Lab 介紹<br />
<br />
<br />
<br />
<br />
三、Controller 裡面的 Method 種類：<br />
<br />
(一)Actino Method：一個 Method 如果是 Public ，原則上為 Action Method ，也就是可直接以網址列接受使用者要求的 Method<br />
(二)一般 Method：一個 Method 如果不是 Public，則為 Controller 內部的 一般 Method ，不可接受使用者要求<br />
(三)Public 的一般 Method：如果 Controller 內的一般 Method 想要設成 Public ，可在 Method 上面加上  [NonAction] ，他就不可以接受使用者要求了， 如下例所示：<br />
<br />
[code]<br />
[NonAction]<br />
public string SimpleMethod()<br />
{<br />
    return "Hi, I am not action method";<br />
}<br />
[/code]<br />
<br />
在這個例子裡，如果我們在網址列打上 localhost/Test/SimpleMethod<br />
則瀏覽器畫面會出現下列錯誤訊息：<br />
<br />
[img]https://www.codeproject.com/KB/aspnet/866143/lab_1.27.png[/img]]]></description>
            <dc:creator>RandomVariable</dc:creator>
            <category>程式設計</category>
            <pubDate>Sun, 22 Jan 2017 23:33:47 +0800</pubDate>
        </item>
    </channel>
</rss>
