1、理解Controller
Controller负责相应浏览器请求,然后将相应的结果发送到浏览器。
例如地址栏输入:http://localhost/Project/Index/
这时候一个名字叫做ProjectController的Controller被触发,然后动作Index被触发,执行相应的操作,返回浏览器请求。
返回的可能是一个特别的View,也可能指向到其它URL。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApplication1.Controllers
{
??? public class ProjectController : Controller
??? {
??????? //
??????? // GET: /Projects/
??????? public ActionResult Index()
??????? {
??????????? // Add action logic here
??????????? return View();
??????? }
??? }
}
上边就是一个Controller,从这里可以看出来一个Controller就是一个类,继承了System.Web.Mvc.Controller类,并继承了一些有用的方法。
2、理解动作(Actions)
一个动作就是Controller中的一个方法,比如上边例子中的Index。
当我们在浏览器中输入一个URL时,首先定位到要执行的Controller,然后定位到要执行的动作。一个Action必须是一个public的方法,当你在Controller中添加一个public方法时,这个方法自动暴露为一个Action。但是这个方法不能是静态方法,而且不能被重写,除此之外,这个方法没有限制。
3、理解Action Results
一个Controller动作(Action)必须要返回一些东西,这就是Action Result,以相应浏览器请求。
ASP.NET MVC框架支持的Action Result类型包括:
- ViewResult – Html和标注.
- EmptyResult – 返回空的结果.
- RedirectResult –转向到一个URL.
- JsonResult – 一个json对象,可以在Ajax中使用.
- JavaScriptResult – javascript脚本.
- ContentResult – 文本.
- FileContentResult –?一个下载文件 (文件字节).
- FilePathResult –一个下载文件的路径.
- FileStreamResult – 一个下载文件(文件流).
所有的Action Result都继承自ActionResult类。
大多数情况下,一个Action返回的是ViewResult,例如1中的Index,他返回的Action Result就是一个ViewResult。
但是这个方法没有返回ViewResult(),他返回的是View()。一般情况下我们可以不直接返回一个Action Result,可以直接调用基类中的如下方法:
有时候我们返回的不是一个Action Result,例如一个日期或者一个数字,这时候返回结果会被自动包装成为一个ContentResult,例如:
public DateTime Index()
??????? {
??????????? return DateTime.Now;
??????? }
他会返回一个日期时间的文本给浏览器。
4、创建Controller
在Controller文件夹上点击右键,添加,Controller:
勾选单选框,为我们自动生成一些Action:
每一个Controller必须要有Controller后缀,后则这个Controller就永远不会被触发。
比如我们创建了下边这个Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApplication1.Controllers
{
??? public class CustomerController : Controller
??? {
??????? //
??????? // GET: /Customer/
??????? public ActionResult Index()
??????? {
??????????? return View();
??????? }
??????? //
??????? // GET: /Customer/Details/5
??????? public ActionResult Details(int id)
??????? {
??????????? return View();
??????? }
??????? //
??????? // GET: /Customer/Create
??????? public ActionResult Create()
??????? {
??????????? return View();
??????? }
??????? //
??????? // POST: /Customer/Create
????????[HttpPost]
??????? public ActionResult Create(FormCollection collection)
??????? {
??????????? try
??????????? {
??????????????? // TODO: Add insert logic here
??????????????? return RedirectToAction("Index");
??????????? }
??????????? catch
??????????? {
??????????????? return View();
??????????? }
??????? }
??????? //
??????? // GET: /Customer/Edit/5
??????? public ActionResult Edit(int id)
??????? {
??????????? return View();
??????? }
??????? //
??????? // POST: /Customer/Edit/5
????????[HttpPost]
??????? public ActionResult Edit(int id, FormCollection collection)
??????? {
??????????? try
??????????? {
??????????????? // TODO: Add update logic here
??????????????? return RedirectToAction("Index");
??????????? }
??????????? catch
??????????? {
??????????????? return View();
??????????? }
??????? }
??? }
}
这个Controller包含了一些我们常用的Action,有Get方法,又有Post方法,这些方法从View获取了一些数据,然后处理,然后返回浏览器。
Controller只是一个类,我们完全可以手工创建这个类。只需要继承System.Web.Mvc.Controller类。
5、创建Action
Action有一些要求:
注:开放式泛型类型,通常泛型的类型被称为开放式类型,.NET的机制规定开放式类型不能被实例化,这样就确保了开放式类型的泛型参数类型在被指定时前,不会被实例化成任何对象(事实上.NET也没办法进行实例化,因为不确定需要分配多少内存给开放式类型)。
如果想阻止一个public方法被暴露为Action,可以给方法添加标注:[NonAction],如:
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
??? public class WorkController : Controller
??? {
??????? [NonAction]
??????? public string CompanySecrets()
??????? {
??????????? return "This information is secret.";
??????? }
??? }
}
OK,关于Controller就介绍到这里了,下一节介绍View。
参考文章:
http://www.asp.net/learn/mvc/tutorial-03-cs.aspx
http://www.asp.net/learn/mvc/tutorial-34-cs.aspx
http://www.asp.net/learn/mvc/tutorial-33-cs.aspx
关键字: action actionresult controller mvc


发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论