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.   public ActionResult Index()
    4.   {
    5.   return View();
    6.   }
    7.   public ActionResult ContentResult()
    8.   {
    9.   return Content("Hi, 我是ContentResult结果");
    10.   }
    11.   public ActionResult EmptyResult()
    12.   {
    13.   //空结果当然是空白了!
    14.   //至于你信不信, 我反正信了
    15.   return new EmptyResult();
    16.   }
    17.   public ActionResult FileResult()
    18.   {
    19.   var imgPath = Server.MapPath("~/demo.jpg");
    20.   return File(imgPath, "application/x-jpg", "demo.jpg");
    21.   }
    22.   public ActionResult HttpNotFoundResult()
    23.   {
    24.   return HttpNotFound("Page Not Found");
    25.   }
    26.   public ActionResult HttpUnauthorizedResult()
    27.   {
    28.   //未验证时,跳转到Logon
    29.   return new HttpUnauthorizedResult();
    30.   }
    31.   public ActionResult JavaScriptResult()
    32.   {
    33.   string js = "alert(\"Hi, I'm JavaScript.\");";
    34.   return JavaScript(js);
    35.   }
    36.   public ActionResult JsonResult()
    37.   {
    38.   var jsonObj = new
    39.   {
    40.   Id = 1,
    41.   Name = "小铭",
    42.   Sex = "男",
    43.   Like = "足球"
    44.   };
    45.   return Json(jsonObj, JsonRequestBehavior.AllowGet);
    46.   }
    47.   public ActionResult RedirectResult()
    48.   {
    49.   return Redirect("~/demo.jpg");
    50.   }
    51.   public ActionResult RedirectToRouteResult()
    52.   {
    53.   return RedirectToRoute(new {
    54.   controller = "Hello", action = ""
    55.   });
    56.   }
    57.   public ActionResult ViewResult()
    58.   {
    59.   return View();
    60.   }
    61.   public ActionResult PartialViewResult()
    62.   {
    63.   return PartialView();
    64.   }
    65.   //禁止直接访问的ChildAction
    66.   [ChildActionOnly]
    67.   public ActionResult ChildAction()
    68.   {
    69.   return PartialView();
    70.   }
    71.   //正确使用ChildAction
    72.   public ActionResult UsingChildAction()
    73.   {
    74.   return View();
    75.   }
    76.   }

MVC3中Action返回类型ActionResult类型的更多相关文章

  1. Controller 中Action 返回值类型 及其 页面跳转的用法

        •Controller 中Action 返回值类型 View – 返回  ViewResult,相当于返回一个View 页面. -------------------------------- ...

  2. Structs2中Action返回json到前台方法

    1.传统方式JSON输出 这一点跟传统的Servlet的处理方式基本上一模一样,代码如下 01 public void doAction() throws IOException{ 02        ...

  3. ASP.NET Core MVC中的IActionFilter.OnActionExecuted方法执行时,Controller中Action返回的对象是否已经输出到Http Response中

    我们在ASP.NET Core MVC项目中有如下HomeController: using Microsoft.AspNetCore.Mvc; namespace AspNetCoreActionF ...

  4. Controller中方法返回值其他类型需要添加jackson依赖

    第一个 第二个: 第三个 https://www.cnblogs.com/codejackanapes/p/5569013.html:json的博客园 springmvc默认的是:2.Jackson: ...

  5. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  6. asp.net mvc 3.0 知识点整理 ----- (2).Controller中几种Action返回类型对比

    通过学习,我们可以发现,在Controller中提供了很多不同的Action返回类型.那么具体他们是有什么作用呢?它们的用法和区别是什么呢?通过资料书上的介绍和网上资料的查询,这里就来给大家列举和大致 ...

  7. 【MVC】关于Action返回结果类型的事儿(上)

    一. ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回结果类型: 1. ...

  8. MVC Action 返回类型[转]

    一.         ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回 ...

  9. ASP.NET MVC – 关于Action返回结果类型的事儿(上)

    原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一.         ASP.NET MVC 1.0 Result 几何? Action的 ...

随机推荐

  1. Linux FTP服务安装和远程登录失败

    问题:本机VPlayer安装pure-ftpd  ftp服务,通过flashfxp从windows连接出现以下错误: [左] 正在连接到 vmare -> IP=192.168.174.133 ...

  2. sharepoint 脚本 强迫以管理员权限运行

    #region 关键代码:强迫以管理员权限运行 $currentWi = [Security.Principal.WindowsIdentity]::GetCurrent() $currentWp = ...

  3. form表单重置

    Jquery中重置表单的错误姿势 $('#yigeform').reset() 正确姿势 $('#yigeform')[0].reset()

  4. linux 定时执行 cron指令

    linux 中的 cron 定时执行命令,先上例子:每间隙两分钟把 "Hello world"写到 /tmp/hello.txt crontab -e */2 * * * * ec ...

  5. 使用jQuery.FileUpload插件和服Backload组件自定义上传文件夹

    在零配置情况下,文件的上传文件夹是根目录下的Files文件夹,如何自定义文件的上传文件夹呢? □ 在web.config中配置 1: <configuration> 2: <conf ...

  6. mac下如何查看指定端口被谁占用并且杀死该进程

    在本地部署 Web 应用时我有遇到过某网络端口已经被其他程序占用的情况,这时候就需要先退出占用该端口的进程,我们可以通过“终端”来实现结束占用某特定端口的进程 1.打开终端,使用如下命令: lsof ...

  7. thinkphp表单上传文件并将文件路径保存到数据库中

    上传单个文件,此文以上传图片为例,上传效果如图所示 创建数据库upload_img,用于保存上传路径 CREATE TABLE `seminar_upload_img` (  `id` int(11) ...

  8. poj 3013 Big Christmas Tree Djistra

    Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...

  9. 在使用Fake framework的时候,为什么有一些函数没有生产mock呢?

    在使用Visual studio 2012 的Fake framework 做单元测试的时候,你会发现有一些函数没有生产Stub 或者 Shim的版本,这可能是由于Fake的一些限制导致的,但如何知道 ...

  10. javascript debut trick, using the throw to make a interrupt(breakpoint) in your program

    console.log('initialize'); try { throw "breakPoint"; } catch(err) {} when I debug the extj ...