MVC3中Action返回类型ActionResult类型

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

ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 版本中提供的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文件

 

 示例代码:   

    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(\"Hi, I'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.   }  

posted on   大西瓜3721  阅读(161)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)

导航

点击右上角即可分享
微信分享提示