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文件 |
示例代码:
- 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();
- }
- }
MVC3中Action返回类型ActionResult类型的更多相关文章
- Controller 中Action 返回值类型 及其 页面跳转的用法
•Controller 中Action 返回值类型 View – 返回 ViewResult,相当于返回一个View 页面. -------------------------------- ...
- Structs2中Action返回json到前台方法
1.传统方式JSON输出 这一点跟传统的Servlet的处理方式基本上一模一样,代码如下 01 public void doAction() throws IOException{ 02 ...
- ASP.NET Core MVC中的IActionFilter.OnActionExecuted方法执行时,Controller中Action返回的对象是否已经输出到Http Response中
我们在ASP.NET Core MVC项目中有如下HomeController: using Microsoft.AspNetCore.Mvc; namespace AspNetCoreActionF ...
- Controller中方法返回值其他类型需要添加jackson依赖
第一个 第二个: 第三个 https://www.cnblogs.com/codejackanapes/p/5569013.html:json的博客园 springmvc默认的是:2.Jackson: ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- asp.net mvc 3.0 知识点整理 ----- (2).Controller中几种Action返回类型对比
通过学习,我们可以发现,在Controller中提供了很多不同的Action返回类型.那么具体他们是有什么作用呢?它们的用法和区别是什么呢?通过资料书上的介绍和网上资料的查询,这里就来给大家列举和大致 ...
- 【MVC】关于Action返回结果类型的事儿(上)
一. ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回结果类型: 1. ...
- MVC Action 返回类型[转]
一. ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回 ...
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一. ASP.NET MVC 1.0 Result 几何? Action的 ...
随机推荐
- PC软件-实用工具 True Launch Bar
True Launch Bar 官网 增强及自定义window任务栏快捷方式管理. 有免费版的Free Launch Bar 官网
- c#的多线程
多线程的使用方法: Thread t = new Thread(new ThreadStart (StartMethod)); t.Start(); private void StartMethod( ...
- lispbox 安装运行.sh的时候出现 lispbox.sh: 2: lispbox.sh: Bad substitution
安装lispbox时使用tar命令将压缩文件解压之后cd进入之后在运行.sh文件时出现了如下情况. $ sh lispbox.sh lispbox.: lispbox.sh: Bad substitu ...
- php 魔术方法__get()和__set()理解
__get()方法,官方手册上是这样解释的 : ' 当调用(自己加的:或设置|赋值)当前环境下未定义或不可见的类属性或方法时,重载方法会被调用.本节后面将使用"不可访问属性(inaccess ...
- PHP之session_start()详解
1.session的工作原理 (1)首先使用session_start()函数进行初始换 (2)当执行PHP脚本时,通过使用$_SESSION超全局变量注册session变量. (3)当PHP脚本执行 ...
- js组件开发流程
html代码 <div id="div1"></div> <div id="div2"></div> <d ...
- ECSHOP模板设置,前台英文后台中文,无需复制
很多做英文站的朋友 只想让前台显示为英文,后台依就保持中文.这个要如何来做呢?网上也看到类似文章,好像还要进行目录复制与覆盖.我下面这个方法更简单,无需复制. 第一步: 通过后台设置实现前台英文.进入 ...
- Extension method for type
扩展其实真的很简单 msdn是这样规定扩展方法的:"扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为 ...
- python连接zookeeper的日志问题
用python连接zookeeper时,在终端里,一直会有zookeeper的日志冒出来,这样会很烦. -- ::,:(: Exceeded deadline by 11ms 解决方法是在连接后设置一 ...
- Django环境搭建和项目创建
1.下载安装python 2.打开shell(windows下cmd),安装虚拟环境工具: "pip install virtualenv".(可以通过“python -m pi ...