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

本文介绍如何使用ASP.NET MVC4创建Web应用项目,包括MVC的基本概念、项目搭建步骤及控制器方法的调用机制。通过具体示例演示了如何设置模型、视图和控制器,并解释了URL请求处理流程。

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)

 

转载于:https://www.cnblogs.com/siri/p/3580693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值