博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.net MVC 之ActionResult
阅读量:4315 次
发布时间:2019-06-06

本文共 2907 字,大约阅读时间需要 9 分钟。

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

   

   

   

转载于:https://www.cnblogs.com/mrfang/p/10707148.html

你可能感兴趣的文章
SQL 通用数据类型
查看>>
UVALive 6145 Version Controlled IDE(可持久化treap、rope)
查看>>
mysql 将两个有主键的表合并到一起
查看>>
底部导航栏-----FragmentTabHost
查看>>
在linux中安装jdk以及tomcat并shell脚本关闭启动的进程
查看>>
apk,task,android:process与android:sharedUserId的区别
查看>>
MySQL 同主机不同数据库之间的复制
查看>>
iOS菜鸟开发 UIView,UIImageView 的点击事件
查看>>
取得GridView被隐藏列的值方法集合
查看>>
第七章例7-14
查看>>
SQL Server 维护计划实现数据库备份(Step by Step)
查看>>
VRPN 介绍及使用
查看>>
MyBatis使用懒加载mybatis-config.xml配置
查看>>
《C++ Primer》第五版课后习题解答_第六章(2)(08-15)
查看>>
c语言第五次作业
查看>>
多线程执行显示进度条的实例
查看>>
【总结】 NOIp2018考时经历记
查看>>
DIY远程控制开关(tiny6410+LED+yeelink+curl)
查看>>
SGU[130] CIrcle
查看>>
深入V8引擎-Time核心方法之win篇(1)
查看>>