波斯马BOSSMA Information Technology

ASP.NET MVC实战体验之项目管理系统(4)

发布时间:2010年3月8日 / 分类:ASP.NET, ASP.NET MVC / 10,337 次浏览 / 评论

上一节详细介绍了Model,包括怎么创建Model,这一节将介绍Controller。

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类型包括:

  1. ViewResult – Html和标注.
  2. EmptyResult – 返回空的结果.
  3. RedirectResult –转向到一个URL.
  4. JsonResult – 一个json对象,可以在Ajax中使用.
  5. JavaScriptResult – javascript脚本.
  6. ContentResult – 文本.
  7. FileContentResult –?一个下载文件 (文件字节).
  8. FilePathResult –一个下载文件的路径.
  9. FileStreamResult – 一个下载文件(文件流).

所有的Action Result都继承自ActionResult类。

大多数情况下,一个Action返回的是ViewResult,例如1中的Index,他返回的Action Result就是一个ViewResult。

但是这个方法没有返回ViewResult(),他返回的是View()。一般情况下我们可以不直接返回一个Action Result,可以直接调用基类中的如下方法:

  • View – 返回一个ViewResult类型的action result.
  • Redirect – 返回一个RedirectResult类型的action result.
  • RedirectToAction –返回一个RedirectToRouteResult 类型的action result.
  • RedirectToRoute – 返回一个RedirectToRouteResult 类型的action result.
  • Json – 返回一个JsonResult 类型的action result.
  • JavaScriptResult –?返回一个 JavaScriptResult.
  • Content –?返回一个 ContentResult类型的action result.
  • File – 通过给这个方法的参数返回一个FileContentResult或FilePathResult或FileStreamResult.
  • 有时候我们返回的不是一个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有一些要求:

  • 必须是public的.
  • 不能是一个静态方法.
  • 不能是一个extension方法.
  • 不能是constructor, getter, or setter.
  • 不能拥有开放式泛型类型.
  • 不是基类中的方法.
  • 不能包含refout参数.
  • 注:开放式泛型类型,通常泛型的类型被称为开放式类型,.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

    本博客所有文章如无特别注明均为原创。
    复制或转载请以超链接形式注明转自波斯马,原文地址《ASP.NET MVC实战体验之项目管理系统(4)

    关键字:

    建议订阅本站,及时阅读最新文章!
    【上一篇】 【下一篇】

    发表评论