ActionResult 派生出以下子类:

  1. ViewResult

    返回一个网页视图

  2. PartialViewResult

    返回一个网页视图,但不适用布局页。

  3. ContentResult

    返回一段字符串文本。和直接返回string字符串没有区别,只不过可以设置返回内容的格式和编码格式。例如:

public string Content()

{

return "<h1>HelloKitty</h1>"; //浏览器显示 HelloKitty

}

 
 

public ActionResult Content2()

{

//return Content("<h1>GoodbyeKitty</h1>"); //浏览器显示 GoodbyeKitty

//指定返回文本的格式与字符编码

return Content("<h1>GoodbyeKitty</h1>",

"text/html",System.Text.Encoding.UTF8);

}

 
 

 
 

  1. JsonResult

    传入一个任意类型的对象,尽可能地将它格式化为JSON格式。

    对于文件或图片类型数据会转换为乱码。

    Dictionary这种键值对类型会被转换成js类,List这类会被转换成js数组

    例如:

class Student

{

public string Name { get; set; }

public int Age { get; set; }

}

 
 

public ActionResult JSON1()

{

var array = new List<Student>();

array.Add(new Student { Name = "小明", Age = 12 });

array.Add(new Student { Name = "小李", Age = 15 });

return Json(array,JsonRequestBehavior.AllowGet);

//JsonRequestBehavior用于指定是否允许GET方式访问,默认只允许POST

 
 

//运行结果:[{"Name":"小明","Age":12},{"Name":"小李","Age":15}]

}

 
 

public ActionResult JSON2()

{

//也可使用匿名内部类来保存数据

return Json(new { name = "test", age = 16, sex = "boy" }, JsonRequestBehavior.AllowGet);

 
 

//运行结果:{"name":"test","age":16,"sex":"boy"}

}

  1. JavaScriptResult

    返回一个JavaScript代码字符串。JavaScript()的效果实际上和Content()是一样的,只不过JavaScript()会自动指定返回文本的内容是application/x-javascript。例如:

public ActionResult JS()

{

return JavaScript("alert('" + DateTime.Now.ToLongTimeString() + "')");

}

 
 

public ActionResult JS2()

{

return Content("alert('" + DateTime.Now.ToLongTimeString() + "')", "application/x-javascript");

//这样写效果和上面完全是一样的

}

 
 

/*

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<script src="http://localhost:5695/home/js2"></script>

<!--可以直接在script标签中填写action的地址-->

</head>

<body></body>

</html>

*/

 
 

  1. FileResult

    返回一个文件,可以通过文件名、文件流、二进制Byte[ ]的形式发送文件,需要指定文件类型。例如:

public ActionResult FILE1()

{

System.IO.Stream fs = System.IO.File.OpenRead(@"test.png");

return File(fs, @"image/png"); //通过流的方法

}

 
 

public ActionResult FILE2()

{

return File(@"test.png", @"image/png"); //通过文件名的方式

}

 
 

public ActionResult FILE3()

{

System.Drawing.Bitmap b = new System.Drawing.Bitmap(100, 100); //创建一张空白图片

System.IO.MemoryStream ms = new System.IO.MemoryStream();

b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

byte[] bytes = ms.GetBuffer();

 
 

return File(bytes, @"image/bmp"); //通过二进制数据的方式

}

 
 

/*

//现在可以直接将地址赋值给img标签来进行显示了

<img src="http://localhost:5695/home/file1" alt="">

*/

 
 

  1. EmptyResult

    返回null,如果Action返回null,则会自动将null转换为EmptyResult

  2. RedirectResult

    使客户端浏览器跳转到指定的URL

  3. RedirectToRouteResult

    RedirectToAction()方法将客户端浏览器跳转的指定的Action

    RedirectToRoute()方法将客户端浏览器跳转到指定的URL,取决于路由

     
     

 
 

附录:MIME

MIME (Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型。

是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。

常见文件格式

文件类型

格式编码

超文本标记语言文件(.html)

text/html

XML文档(.xml)

Text/xml

普通文本(.txt)

Text/plain

PDF文档(.pdf)

application/pdf

Word文档(.docx)

application/msword

PNG图像(.png)

image/png

GIF图形(.gif)

image/gif

JPEG图形(.jpeg , .jpg)

image/jpeg

 
 

 
 

 
 

Asp.net MVC 之ActionResult的更多相关文章

  1. [转载]深入理解ASP.NET MVC之ActionResult

    Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...

  2. Asp.net MVC 之 ActionResult

    Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...

  3. asp.net mvc之ActionResult

    Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的, 但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的, ActionR ...

  4. ASP.NET MVC自定义ActionResult实现文件压缩

    有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载 using System; using System.Collections; usi ...

  5. ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出

    之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itexts ...

  6. ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别

    如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult

  7. Asp.net MVC 控制器ActionResult的例子

    ActionResult 父类型 ViewResult View() 多重载应用 PartialViewResult PartialView() 部分试图 New EmptyResult()  空 如 ...

  8. ASP.NET MVC中ActionResult的不同返回方式

    1.返回视图 return View();//返回方法名对应的视图 return View("aaa");//返回名称为aaa的视图 2.返回文本内容 return Content ...

  9. 【转】ASP.NET MVC学习笔记-Controller的ActionResult

    1. 返回ViewResult public ActionResult Index()   {       ViewData["Message"] = "Welcome ...

随机推荐

  1. spring以及json,fastjson和jackson

    (一) @RestController 以及 @RequestBody spring的这两个注解都需要使用对应的 message-converters 实现 pojo到字符串的转换, 需要配置实现了  ...

  2. 【Beta】“北航社团帮”测试报告——小程序v2.0与网页端v1.0

    目录 测试计划.过程和结果 后端测试--单元测试与覆盖率 后端测试--压力测试 展示部分数据 平均数据 前端测试--小程序v2.0 授权登录与权限检查 新功能的测试 兼容性测试 性能测试 前端测试-- ...

  3. 【转】Linux 系统如何处理名称解析

    原文写的很好:https://blog.arstercz.com/linux-%E7%B3%BB%E7%BB%9F%E5%A6%82%E4%BD%95%E5%A4%84%E7%90%86%E5%90% ...

  4. 带缓存的基于DateTimeFormatter的日期格式化工具类

    JAVA中的SimpleDateFormat是非线程安全的,所有在1.8的JDK版本里提供了线程安全的DateTimeFormatter类,由于是线程安全的,故我们可以将此类缓存起来多次利用提高效率. ...

  5. Spring源码解析之PropertyPlaceholderHelper(占位符解析器)

    Spring源码解析之PropertyPlaceholderHelper(占位符解析器) https://blog.csdn.net/weixin_39471249/article/details/7 ...

  6. Python3基础 from...import...as 解决局部导入时的函数名重复问题

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  7. spring与springMVC的父子容器关系

    背景和概述 在spring与springMVC中通过IOC可以管理bean对象,有两个配置文件可以配置ioc spring的配置文件applicationContext.xmlspringMVC的配置 ...

  8. ubuntu连接多个realsense d435

    ubuntu连接多个realsense d435 import pyrealsense2 as rs import numpy as np import cv2 import time import ...

  9. socket 发送字符串0x00时被截断

    发送数据如下: aa 02 02 00 00 00 6f 6b 02 00 00 00 55 数据是以字符数组的形式(char msg[])存储发送的,send时发送长度填写的strlen(msg), ...

  10. async异步流程控制神器

    async https://www.npmjs.com/package/async Async is a utility module which provides straight-forward, ...