ASP.NET MVC 3 Controller

本文详细解析了ASP.NET MVC框架中Controller的作用及其处理客户端请求的过程,包括Controller的注意事项、ActionResult类的用途及具体实现案例。通过实例代码展示了如何使用不同类型的ActionResult来响应各种请求场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上两篇文章讲的是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运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值