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. PC软件-实用工具 True Launch Bar

    True Launch Bar 官网 增强及自定义window任务栏快捷方式管理. 有免费版的Free Launch Bar 官网

  2. c#的多线程

    多线程的使用方法: Thread t = new Thread(new ThreadStart (StartMethod)); t.Start(); private void StartMethod( ...

  3. lispbox 安装运行.sh的时候出现 lispbox.sh: 2: lispbox.sh: Bad substitution

    安装lispbox时使用tar命令将压缩文件解压之后cd进入之后在运行.sh文件时出现了如下情况. $ sh lispbox.sh lispbox.: lispbox.sh: Bad substitu ...

  4. php 魔术方法__get()和__set()理解

    __get()方法,官方手册上是这样解释的 : ' 当调用(自己加的:或设置|赋值)当前环境下未定义或不可见的类属性或方法时,重载方法会被调用.本节后面将使用"不可访问属性(inaccess ...

  5. PHP之session_start()详解

    1.session的工作原理 (1)首先使用session_start()函数进行初始换 (2)当执行PHP脚本时,通过使用$_SESSION超全局变量注册session变量. (3)当PHP脚本执行 ...

  6. js组件开发流程

    html代码 <div id="div1"></div> <div id="div2"></div> <d ...

  7. ECSHOP模板设置,前台英文后台中文,无需复制

    很多做英文站的朋友 只想让前台显示为英文,后台依就保持中文.这个要如何来做呢?网上也看到类似文章,好像还要进行目录复制与覆盖.我下面这个方法更简单,无需复制. 第一步: 通过后台设置实现前台英文.进入 ...

  8. Extension method for type

    扩展其实真的很简单 msdn是这样规定扩展方法的:"扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为 ...

  9. python连接zookeeper的日志问题

    用python连接zookeeper时,在终端里,一直会有zookeeper的日志冒出来,这样会很烦. -- ::,:(: Exceeded deadline by 11ms 解决方法是在连接后设置一 ...

  10. Django环境搭建和项目创建

    1.下载安装python 2.打开shell(windows下cmd),安装虚拟环境工具:  "pip install virtualenv".(可以通过“python -m pi ...