ASP.NET MVC 学习1、新增Controller,了解MVC运行机制

1,turorial ,根据链接教程新建一个MVC项目

http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

First Page:

            

2,M-V-C:

  Models: 存放数据模型(Model Data),验证接收的数据(Validation Logic)

  Views:  用户看到的HTML页面 (dynamically genetate HTML Page)

  Controls:处理用户请求,从Models中检索数据以后,指定View页面输出(specify View templates)到浏览器

Add a new controller:

3, 了解MVC运行机制

   MVC 根据不同的URL请求,MVC框架会实例化Controller方法,然后调用相应controller class 中的相应的action methods .默认的调用路径是:/[Controller]/[ActionName]/[Parameters]

http://localhost:9898/helloWorld/index /helloworld/index

http://localhost:9898/helloWorld/welcome /helloworld/welcome

上面的路径,MVC的路由机制会从helloworldController控制器中找到index方法和welcome方法,直接显示HTML页面。路径中没有Parameters

 

更新Controller中的Welcome方法如下

public string Welcome(string name, int numTimes = 1) {

     return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);}

浏览器地址手动更新为:http://localhost:9898/helloWorld/Welcome?name=Spring&numtimes=8

参数即被传递到页面中:

 

参考:

http://www.asp.net/mvc/tutorials/mvc-4

http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m1-intro&mode=live&clip=0&course=mvc4-building (mvc4 video)

 

posted @ 2014-03-04 16:51  sirili  阅读(1183)  评论(0编辑  收藏  举报