actionresult的返回类型

来源:互联网 发布:边境牧羊犬多聪明 知乎 编辑:程序博客网 时间:2024/05/17 04:41

MVC3中Action返回类型ActionResult在System.Web.Mvc命名空间中.这些包含在控制器中的方法,我们称为控制器中的 Action,比如:HomeController 中的 Index 方法就是一个 Action,这些 Action 的作用就是处理请求,然后返回对请求的处理结果。

ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 版本中提供的ActionResult派生类:

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

 

 示例代码:   

view plainprint?
  1. public class ActionResultController Controller  
  2.   
  3.   {  
  4.   
  5.   public ActionResult Index()  
  6.   
  7.   {  
  8.   
  9.   return View();  
  10.   
  11.   }  
  12.   
  13.   public ActionResult ContentResult()  
  14.   
  15.   {  
  16.   
  17.   return Content("Hi, 我是ContentResult结果");  
  18.   
  19.   }  
  20.   
  21.   public ActionResult EmptyResult()  
  22.   
  23.   {  
  24.   
  25.   //空结果当然是空白了!  
  26.   
  27.   //至于你信不信, 我反正信了  
  28.   
  29.   return new EmptyResult();  
  30.   
  31.   }  
  32.   
  33.   public ActionResult FileResult()  
  34.   
  35.   {  
  36.   
  37.   var imgPath Server.MapPath("~/demo.jpg");  
  38.   
  39.   return File(imgPath, "application/x-jpg", "demo.jpg");  
  40.   
  41.   }  
  42.   
  43.   public ActionResult HttpNotFoundResult()  
  44.   
  45.   {  
  46.   
  47.   return HttpNotFound("Page Not Found");  
  48.   
  49.   }  
  50.   
  51.   public ActionResult HttpUnauthorizedResult()  
  52.   
  53.   {  
  54.   
  55.   //未验证时,跳转到Logon  
  56.   
  57.   return new HttpUnauthorizedResult();  
  58.   
  59.   }  
  60.   
  61.   public ActionResult JavaScriptResult()  
  62.   
  63.   {  
  64.   
  65.   string js "alert(\"HiI'm JavaScript.\");";  
  66.   
  67.   return JavaScript(js);  
  68.   
  69.   }  
  70.   
  71.   public ActionResult JsonResult()  
  72.   
  73.   {  
  74.   
  75.   var jsonObj new  
  76.   
  77.   {  
  78.   
  79.   Id 1 
  80.   
  81.   Name "小铭" 
  82.   
  83.   Sex "男" 
  84.   
  85.   Like "足球"  
  86.   
  87.   };  
  88.   
  89.   return Json(jsonObj, JsonRequestBehavior.AllowGet);  
  90.   
  91.   }  
  92.   
  93.   public ActionResult RedirectResult()  
  94.   
  95.   {  
  96.   
  97.   return Redirect("~/demo.jpg");  
  98.   
  99.   }  
  100.   
  101.   public ActionResult RedirectToRouteResult()  
  102.   
  103.   {  
  104.   
  105.   return RedirectToRoute(new  
  106.   
  107.   controller "Hello"action ""  
  108.   
  109.   });  
  110.   
  111.   }  
  112.   
  113.   public ActionResult ViewResult()  
  114.   
  115.   {  
  116.   
  117.   return View();  
  118.   
  119.   }  
  120.   
  121.   public ActionResult PartialViewResult()  
  122.   
  123.   {  
  124.   
  125.   return PartialView();  
  126.   
  127.   }  
  128.   
  129.   //禁止直接访问的ChildAction  
  130.   
  131.   [ChildActionOnly]  
  132.   
  133.   public ActionResult ChildAction()  
  134.   
  135.   {  
  136.   
  137.   return PartialView();  
  138.   
  139.   }  
  140.   
  141.   //正确使用ChildAction  
  142.   
  143.   public ActionResult UsingChildAction()  
  144.   
  145.   {  
  146.   
  147.   return View();  
  148.   
  149.   }  
  150.   
  151.   }  
0 0