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. Java 最佳命名规则记录

    类名:使用双驼峰规则.如 TestService 包名:命名全小写,且使用小写 如使用 web.csvdata.util 而不是 web.csvData.util 或 web.csvdata.util ...

  2. 论文阅读:Learning Attention-based Embeddings for Relation Prediction in Knowledge Graphs(2019 ACL)

    基于Attention的知识图谱关系预测 论文地址 Abstract 关于知识库完成的研究(也称为关系预测)的任务越来越受关注.多项最新研究表明,基于卷积神经网络(CNN)的模型会生成更丰富,更具表达 ...

  3. VBA_headers_mapping

    Header Mapping--应对 Report Headers 的变化 Author : Collin_PXY 背景 在 RPA工作中,稳定的规则非常重要,因为 RPA项目就是基于规则而进行的,但 ...

  4. Linux 笔记1

    linux netstat -an | grep 8081 查看端口进程 window netstat -ano|findstr "1433" taskkill -pid ** - ...

  5. shell中数字、字符串、文件比较测试

    1.逻辑运算符:与&&     或||    非!  &&:双目操作符:与运算中:如果第一个数为假,结果一定为假   ==> 短路操作符 ||:双目操作符:或运算 ...

  6. linux文件的3个时间和7种文件类型

    linux文件的三个时间:  atime: access time --最近访问时间. ctime: change time --最近改变时间. mtime:modify time --最近修改时间. ...

  7. 两年经验拿到蚂蚁金服,字节offer,附上金九银十BAT面试核心知识点整理

    前言 我自己是本科毕业后在老东家干了两年多,老东家算是一家"小公司"(毕竟这年头没有 BAT 或 TMD 的 title 都不好意思报出身),毕业这两年多我也没有在大厂待过,因此找 ...

  8. 详细了解IDM的队列功能

    队列的种类 IDM(Internet Download Manager)下载器的队列分为2种:主要下载队列和同步队列.此外,我们也可以自己创建附加队列. 在左边的[分类]窗口中,黄色的图标为主要下载队 ...

  9. 03生成微博授权URL接口

    1.创建apps/oauth模块进行oauth认证 '''2.1 在apps文件夹下新建应用: oauth''' cd syl/apps python ../manage.py startapp oa ...

  10. 【原创】视频+文字:详解VBA解决数独问题

    [说在前面]: 之前,我在微信朋友圈看到一个同事发了一个状态,说的是她在家辅导孩子做作业,一个数独的题目,好像没有做出来.我看了下,我也做不出来,后来仔细想了下,花了两个多小时时间,用Python编了 ...