標籤

2017年5月25日 星期四

ASP.NET MVC 5 - Adding a Controller

᳓前言
本篇文章將"Getting Started",與"Adding a Controller"合併整理而成。

᳓重點整理
MVC為Model-View-Controller的縮寫,是一種開發應用程式的架構,具有可測試、容易維護等優點。
  • Model負責處理資料
  • View負責產生HTML的介面
  • Controller負責進行資料交換、控制程式運作流程
使用者透過瀏覽器向伺服器送出request後,經由View將request傳送給Controller,Controller會跟Model取得所需的資料並傳送給View,經由View回傳response至瀏覽器並呈現。

᳓實作1. Create a project
1. File -> New -> Project
2. C# -> Web -> ASP.NET Web Application -> OK
3. MVC -> 取消勾選"Host in the cloud" -> OK

᳓實作2. 新增Controllers/HelloWorldController.cs
1. 右鍵Controller folder -> Add -> Controller
2. MVC 5 Controller - Empty -> Add
3. 輸入HelloWorld -> Add

᳓實作3. 修改Controllers/HelloWorldController.cs
在App_Start/RouteConfig.cs中預設的routing格式為/[Controller]/[ActionName]/[Parameters]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class HelloWorldController : Controller
{
// GET: HelloWorld
public string Index()
{
return "This is my <b>Default</b> action";
}
// GET: HelloWorld/Welcome/
public string Welcome()
{
return "This is the <b>Welcome</b> action";
}
}
}

1. 按下"F5"或"Ctrl+F5"執行即會自動開啟預設瀏覽器,並開啟初始頁面
2. 在網址列輸入"http://{localhost:port}/HelloWorld",大小寫不拘,執行HellowWorld的預設action(Index)。
3. 在網址列輸入"http://{localhost:port}/HelloWorld/Welcome",大小寫不拘,執行HellowWorld的Welcome action。(之所以要使用HtmlEncode()是為了防止惡意的輸入)

᳓實作4. 修改Controllers/HelloWorldController.cs的內容-query string
修改Welcome action,它將可傳遞兩個query strings作為Welcome action參數。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class HelloWorldController : Controller
{
// GET: HelloWorld
public string Index()
{
return "This is my <b>Default</b> action";
}
// GET: HelloWorld/Welcome/
public string Welcome(string name, int numTimes = 1) // can set the default value
{
return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
}
}
}

網址列輸入"http://{localhost:port}/HelloWorld/Welcome?name=Scott&numTimes=4"。(name、numTimes大小寫不拘;若numTimes不輸入已預設為1)

᳓實作5. 修改Controllers/HelloWorldController.cs的內容-parameter
修改numTimes為ID,則會去比對RoutConfig.cs中的設定,根據RegisterRoutes方法中的url:"{controller/{action}/{id}}"便能夠判斷ID為傳遞的參數。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class HelloWorldController : Controller
{
// GET: HelloWorld
public string Index()
{
return "This is my <b>Default</b> action";
}
// GET: HelloWorld/Welcome/
public string Welcome(string name, int ID = 1)
{
return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);
}
}
}

網址列輸入"http://{localhost:port}/HelloWorld/Welcome/3?name=Rick",傳遞parameter及query string作為Welcome action的參數。(由於parameter設為int,若輸入字母則會無法辨識)

᳓實作6. 修改App_Start/RoutConfig.cs的內容-parameters
新增一個name:"Hello"的route,便可新增一個新的url判斷,兩者可同時並存。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication1
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Hello",
url: "{controller}/{action}/{name}/{id}"
);
}
}
}
view raw RouteConfig.cs hosted with ❤ by GitHub

網址列輸入"http://{localhost:port}/HelloWorld/Welcome/Scott/3",傳遞parameters作為Welcome的參數。(若輸入"http://{localhost:port}/HelloWorld/Welcome/3?name=Scott"會有同樣的顯示結果)

沒有留言:

張貼留言