上两篇文章讲的是View层的Razor视图引擎,那么今天咱就讲另一个玩玩,什么呢 ? Controller

首先看一下ASP.net MVC 的请求过程 看一下客户端向服务器发送一个请求后服务器 做了哪些事情吧!

有些内容图上我已经标的很清楚了,我再这就不再一一描述了,直接看图就OK了。

下面是今天要说的Controller

      从上图可以看出客户端请求通过IIS已经MVC中路由机制的解析后就扔给MVC 的controller处理了,从这点可以看出controller(控制器)在MVC中扮演着处理客户端请求的角色。

对于controller有以下几个注意事项:

> 1. 必须实现System.Web.Mvc.IController 接口 --- 但是正常情况小我们是直接继承System.Web.Mvc.Controller 类 ,这样就避免了我们再完全的实现IController接口了,但是如果您的项目需求现有的controller类不能满足那就自己实现一下IController 接口吧!

> 2. 必须要以Controller结尾, 比如 有这么个url地址:http://www.cnblogs/wlitsoft/blogs/123 路由解析后的结果是找wlitsoftController 类。

> 3. Controller 类中有大量的返回值为ActionResult 的方法,这写方法就是所谓的Action,然后Controller通过不同的Action来处理具体的客户端请求,比如还是上面的那个url地址,就是wlitSoftController 会找  blogs 这个 Action 方法 然后参数为123。

说到ActionResult 那就不能不好好的说说,好下面咱就好好的说下ActionResult

类名 抽象类 父类 功能
ContentResult     根据内容的类型和编码,数据内容.
EmptyResult     空方法.
FileResult abstract   写入文件内容,具体的写入方式在派生类中.
FileContentResult   FileResult 通过 文件byte[] 写入文件.
FilePathResult   FileResult 通过 文件路径 写入文件.
FileStreamResult   FileResult 通过 文件Stream 写入文件.
HttpUnauthorizedResult     抛出401错误
JavaScriptResult     返回javascript文件
JsonResult     返回Json格式的数据
RedirectResult     使用Response.Redirect重定向页面
RedirectToRouteResult     根据Route规则重定向页面
ViewResultBase abstract   调用IView.Render()
PartialViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法. 重写了父类的FindView方法. 寻找用户控件.ascx文件
ViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法. 重写了父类的FindView方法. 寻找页面.aspx文件

下面咱就同代码的方式依次证明验证一下这些ActionResult
首先,建一个MVC 3 项目 这里就不再演示了,不会的的可以看下我以前写的文章。

第二步 新建一个Controller

添加一个名为CtextController 的 Controller

第三步 写代码呗

  1 namespace Mvc3App1.Controllers
  2 {
  3     public class CTestController : Controller
  4     {
  5         //
  6         // GET: /CTest/
  7 
  8         public ActionResult Index()
  9         {
 10             return View();
 11         }
 12 
 13         public ActionResult ContentResult()
 14         {
 15             return Content("Hi, 我是ContentResult结果");
 16             //return Content("<script>alert('abc')</script>");
 17         }
 18 
 19         public ActionResult EmptyResult()
 20         {
 21             //就是一个空白页面 真的是一个什么都没有的页面
 22             return new EmptyResult();
 23         }
 24 
 25         public ActionResult FileResult()
 26         {
 27             var imgPath = Server.MapPath("~/images/1.jpg");
 28             return File(imgPath, "application/x-jpg", "1.jpg");
 29         }
 30 
 31         public ActionResult HttpNotFoundResult()
 32         {
 33             return HttpNotFound("Page Not Found");
 34         }
 35 
 36         public ActionResult HttpUnauthorizedResult()
 37         {
 38             //未验证时,跳转到Logon
 39             return new HttpUnauthorizedResult();
 40         }
 41 
 42         public ActionResult JavaScriptResult()
 43         {
 44             //string js = "alert(\"Hi, I'm JavaScript.\");";
 45             //return JavaScript(js);
 46             string s = "$('#divResultText').html('JavaScript Passed')";
 47             return JavaScript(s);
 48         }
 49 
 50         public ActionResult JsonResult()
 51         {
 52             var jsonObj = new
 53             {
 54 
 55                 Id = 1,
 56 
 57                 Name = "小铭",
 58 
 59                 Sex = "",
 60 
 61                 Like = "足球"
 62 
 63             };
 64             return Json(jsonObj, JsonRequestBehavior.AllowGet);
 65         }
 66 
 67         public ActionResult RedirectResult()
 68         {
 69             return Redirect("~/images/1.jpg");
 70         }
 71 
 72         public ActionResult RedirectToRouteResult()
 73         {
 74             return RedirectToRoute(new
 75             {
 76                 controller = "Hello",
 77                 action = ""
 78             });
 79         }
 80 
 81         public ActionResult ViewResult()
 82         {
 83             return View();
 84         }
 85 
 86         public ActionResult PartialViewResult()
 87         {
 88             return PartialView();
 89         }
 90         //禁止直接访问的ChildAction
 91         [ChildActionOnly]
 92         public ActionResult ChildAction()
 93         {
 94 
 95             return PartialView();
 96 
 97         }
 98 
 99         //正确使用ChildAction
100         public ActionResult UsingChildAction()
101         {
102 
103             return View();
104 
105         }
106     }

 部分运行结果:

  index 运行结果

contentResult运行结果

 

HttpUnauthorizedResult运行结果

 

FIleResult运行结果

      寻人启事  : 上面的JavascriptResult 和 JsonResult 两个 没有测试成功!我查MSDN 也没查到为什么!如果有知道此错误的请您与我联系!如果有了解决方法我将及时更新本博文

 

 posted on 2012-05-28 11:34  WlitSoft  阅读(9673)  评论(9编辑  收藏  举报