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.
入门文章
C1
C2
C3
MVC官方入门例子
// GET: /HelloWorld/ #region 为Controller类添加Action方法 /*MVC默认的Mapping format*/ public string Index() //调用方式 Controller前缀/Index { return "this is default 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
Welcome
- <%for (int i = 0; i <= Convert.ToInt32(ViewData["numtime"]); i++) { %>
- <%=ViewData["Message"].ToString()%> <%}%>