這節我們讓Asp.netMVC真正的跑起來
我們自己建立一個新的Controller
開始行動:
在Controllers中建立一個MVC Controller Class,個人宣傳一下.就叫EiceController
附註一下,這裡是個純廣告,沒有興趣可略過此行:www.eice.com.cn為您建立Web2.0社交網站
預設產生的程式碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication2.Controllers
{
/**//// <summary>
/// 記不記得前面講過的,所有Controller都要繼承於
/// Controller類
/// </summary>
public class EiceController : Controller
{
public void Index(string id) {
}
}
}
當然,除了Controller我們還要建個View
先在Views中建個Eice檔案夾
然後我們要建個Index.aspx
注意了:要建MVC View (Content) Page,如果你要使用母板頁就選用Content Page,反之選用一般Page即可
MVC的Aspx檔案與傳統的WebForm的Aspx檔案有所不同
我們將EiceController的Index寫為 public void Index(string id) {
ViewData["qs"] = id;
RenderView("Index");
}
在View即/Views/Eice/Index.aspx中寫內容<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<%=ViewData["qs"] %>
</asp:Content>
接下來我們訪問
/eice/index/helloeice
也許你會發現,在頁面上出現了helloeice
由上面兩段程式可以看出
string id用於接收QueryString["id"] 其實Action中的參數除了能接收QueryString以外也是可以接收Forms的
這裡不做過多說明了,在後文中會有介紹
ViewData是一個頁面間的IDictionary用於Controller向View傳遞資料
這樣View與Controller就可以協作完成顯示頁面與邏輯處理的工作了
Asp.net Mvc Framework 系列