ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为一个ContentResult类型。默认的ControllerActionInvoker调用ActionResult.ExecuteResult方法生成应答结果。

ActionResult是一个抽象类, 在Action中返回的都是其派生类,具体如下:

类名 抽象类 父类 功能
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文件

示例代码:

public class ActionResultController : Controller
{ public ActionResult Index()
{
return View();
} public ActionResult ContentResult()
{
return Content("Hi, 我是ContentResult结果");
} public ActionResult EmptyResult()
{
return new EmptyResult();
} public ActionResult FileResult()
{
var imgPath = Server.MapPath("~/demo.jpg");
return File(imgPath, "application/x-jpg", "demo.jpg");
} public ActionResult HttpNotFoundResult()
{
return HttpNotFound("Page Not Found");
} public ActionResult HttpUnauthorizedResult()
{
//未验证时,跳转到Logon
return new HttpUnauthorizedResult();
} public ActionResult JavaScriptResult()
{
string js = "alert(\"Hi, I'm JavaScript.\");";
return JavaScript(js);
} public ActionResult JsonResult()
{
var jsonObj = new
{
Id = 1,
Name = "小铭",
Sex = "男",
Like = "足球"
}; return Json(jsonObj, JsonRequestBehavior.AllowGet);
} public ActionResult RedirectResult()
{
return Redirect("~/demo.jpg");
} public ActionResult RedirectToRouteResult()
{
return RedirectToRoute(new
{
controller = "Hello",
action = ""
});
} public ActionResult ViewResult()
{
return View();
} public ActionResult PartialViewResult()
{
return PartialView();
} //禁止直接访问的ChildAction
[ChildActionOnly]
public ActionResult ChildAction()
{
return PartialView();
} //正确使用ChildAction
public ActionResult UsingChildAction()
{
return View();
} }

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

  1. ActionResult 的返回类型

    大多数操作方法会返回从 ActionResult 中派生的类的实例. ActionResult 类是所有操作结果的基础. 不过,也存在不同的操作结果类型,具体取决于操作方法执行的任务. 例如,最常见的 ...

  2. ASP.NET Core WebAPI控制器返回类型的最佳选项

    前言 从.NET Core 2.1版开始,到目前为止,控制器操作可以返回三种类型的WebApi响应.这三种类型都有自己的优点和缺点,但都缺乏满足REST和高可测性的选项. ASP.NET Core中可 ...

  3. asp.net core系列 38 WebAPI 返回类型与响应格式--必备

    一.返回类型 ASP.NET Core 提供以下 Web API Action方法返回类型选项,以及说明每种返回类型的最佳适用情况: (1) 固定类型 (2) IActionResult (3) Ac ...

  4. MVC3中Action返回类型ActionResult类型

    MVC3中Action返回类型ActionResult在System.Web.Mvc命名空间中.这些包含在控制器中的方法,我们称为控制器中的 Action,比如:HomeController 中的 I ...

  5. MVC控制器常用方法返回类型

    控制器的常用方法 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  6. C# web api 返回类型设置为json的两种方法

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  7. ASP.NET MVC5 学习笔记-1 控制器、路由、返回类型、选择器、过滤器

    [TOC] 1. Action 1.1 新建项目 新建项目->Web->Asp.net Web应用程序,选择MVC,选择添加测试. 在解决方案上右键,选择"管理NuGet程序包& ...

  8. MVC5控制器、路由、返回类型、选择器、过滤器

    ASP.NET MVC5 学习笔记-1 控制器.路由.返回类型.选择器.过滤器   [TOC] 1. Action 1.1 新建项目 新建项目->Web->Asp.net Web应用程序, ...

  9. Asp.Net Mvc 返回类型总结

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

随机推荐

  1. 消失的两个数字(1-N缺两个数)

    给定一个数组,包含从 1 到 N 所有的整数,但其中缺了两个数字.你能在 O(N) 时间内只用 O(1) 的空间找到它们吗? 以任意顺序返回这两个数字均可. 示例 1: 输入: [1]输出: [2,3 ...

  2. binary hacks读数笔记(ld 链接讲解 一)

    首先我们先看两段代码: a.c extern int shared; int main(){ int a=100; swap(&a,&shared); } b.c int shared ...

  3. shell中if/seq/for/while/until

    1.if语句格式:  if 判断条件:then statement1 statement2 fi; 例子: 判断/test/a普通文件是否存在,存在则输出yes,不存在则输出no,并创建.  #! / ...

  4. 用rsync备份一台linux服务器上的数据

    rsync是安装完linux后都会自带的,在机器上运行rsync命令看是否有安装即可 备份到远程服务器 这里介绍的rsync的用途是备份一台linux服务器上的数据到另外一台机器 环境 将需要备份机器 ...

  5. 测试_QTP使用

    1.Qtp是什么? QTP是Quick Test Professional的简称,是一种自动测试工具.使用QTP的目的是想用它来执行重复的自动化测试,主要是用于回归测试和测试同一软件的新版本.(百度百 ...

  6. SpringBoot 之 @ControllerAdvice 拦截异常并统一处理

    在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@ModelAttribute,并应用到所有@Requ ...

  7. Linux文件监控工具——inotify-tools

    举例: ip.txt内容如下: 10.1.1.11 root 123 10.1.1.22 root 111 10.1.1.33 root 123456 10.1.1.44 root 54321 写法1 ...

  8. phpstorm 远程调式 php

    https://cloud.tencent.com/developer/article/1561767 超时设置 fastcgi: ``` 1. apache module的情况下: 修改配置文件 h ...

  9. jq判断input 复选框有没有选

    选中了返回true ,没选中返回false$("input[type='checkbox']").is(':checked'):

  10. Codeforces1009F Dominant Indices

    dsu on tree 题目链接 点我跳转 题目大意 给定一棵以 \(1\) 为根,\(n\) 个节点的树.设\(d(u,x)\) 为 \(u\) 子树中到 \(u\) 距离为 \(x\) 的节点数. ...