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

1,turorial ,根据连接教程新建一个MVC项目浏览器

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

First Page:框架

            

2,M-V-C:asp.net

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

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

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

Add a new controller:.net

3, 了解MVC运行机制code

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

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)

相关文章
相关标签/搜索