Model:資料和商務規則 data and business rules
View: 結果展示 output and representation
Controller: 把使用者輸入 轉變成 Model能處理的資料
訪問localhost/Home/index.aspx,
實際機制:HomeControl.cs中的 Action index;
返回機制:返回Views/Controller/index.aspx
總結:請求aspx時,是請求control類下的action方法
返回views時,是返回View目錄下的Control類的action.aspx.
入門文章
C1http://www.cnblogs.com/QLeelulu/archive/2008/09/30/1302462.html
C2http://www.cnblogs.com/QLeelulu/archive/2008/10/03/1303521.html
C3http://www.cnblogs.com/QLeelulu/archive/2008/10/03/1303612.html
MVC官方入門例子
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
// GET: /HelloWorld/ #region 為Controller類添加Action方法 /*MVC預設的Mapping format*/ public string Index() //調用方式 Controller首碼/Index { return "this is <b>default</b> action"; } //public string Welcome() //調用方式 Controller首碼/Welcome //{ // return "this is Welcome action"; //} //http://localhost:7391/HelloWorld/Welcome?name=Scott&numtime=1 //調用時,url的參數名必須和形參同名 public string Welcome(string name, int numtime) //在Control中不許出現函數重載, { return "Hello " + name + "numTime is:" + numtime.ToString(); } #endregion
△ Controller解析URL和參數, 將結果寫到ViewData(索引值對),ViewBag中,View再產生結果頁面
public class HelloWorldController:Controller{ public ActionResult Welcome(string name, int numtime) { ViewData["Message"] = "Hello " + name; ViewData["Numtime"] = numtime; return View(); }}
WelCome.aspx
<h2>Welcome</h2> <ul> <%for (int i = 0; i <= Convert.ToInt32(ViewData["numtime"]); i++) {%> <li><%=ViewData["Message"].ToString()%></li> <%}%> </ul>