Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的,

但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的,

ActionResult是一个抽象类,所以具体的工作还是由很多个子类来完成,

具体的子类有

EmptyResult,

ContentResult

(通过Content,ContentEncoding,ContentType 分别设置返回的内容,字符编码格式以及媒体类型),

FileResult(FileContentResult,FilePathResult,FileStreamResult),

<p>Use this area to provide additional information.</p>
<a href="@Url.Action("ImagePath1", new { id="1" })">下载</a> <img src="@Url.Action("ImagePath1", new { id="1" })" /> <img src="@Url.Action("ImagePath", new { id="1" })" /> <img src="@Url.Action("ImageContent", new { id="1" })" /> <img src="@Url.Action("ImageStream", new { id="1" })" />
        public ActionResult ImagePath(string id)
{
string path = Server.MapPath("/images/" + id + ".jpeg");
return File(path, "image/jpeg");
} public ActionResult ImagePath1(string id)
{
string path = Server.MapPath("/images/" + id + ".jpeg");
return File(path, "image/jpeg", "下载");
} public ActionResult ImageContent(string id)
{
string path = Server.MapPath("/images/" + id + ".jpeg");
byte[] heByte = null;
using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
{
int fsLen = (int)fsRead.Length;
heByte = new byte[fsLen];
int r = fsRead.Read(heByte, 0, heByte.Length);
}
return File(heByte, "image/jpeg");
}
public ActionResult ImageStream(string id)
{
string path = Server.MapPath("/images/" + id + ".jpeg");
FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read); return File(fsRead, "image/jpeg"); }

JavaScriptResult,

返回一段js,且媒体类型是application/x-javascript,

JsonResult,

返回Json数据,默认ContentType为Application/json.

HttpStatusCodeResult,

具体是通过设置response的StatusCode和StatusDescription来完成输出

RedirectResult,RedirectToRouteResult,

内部是通过Response的Redirect/RedirectPermanent来完成操作,

redirectresult具有两个属性permanent和URL,URL可以是绝对的地址也可以是相对地址,permanent决定了重定向是暂时的还是永久的重定向,

两种重定向的不同点事搜索引擎会根据永久重定向来更新自己的索引,

RedirectToRouteResult较RediretResult多了一步根据路由计算出来这个URL值,

所以RedirectToRouteResult没有URL属性,却包含RouteName以及RouteValues属性,

ViewResult.

ViewResult是一个特殊的ActionResult,但也是最复杂的一个

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实现文件压缩

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

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

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

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

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

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

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

  7. Asp.net MVC 之ActionResult

    ActionResult 派生出以下子类: ViewResult 返回一个网页视图 PartialViewResult 返回一个网页视图,但不适用布局页. ContentResult 返回一段字符串文 ...

  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. 【慕课网实战】Spark Streaming实时流处理项目实战笔记十三之铭文升级版

    铭文一级: 第10章 Spark Streaming整合Kafka spark-submit \--class com.imooc.spark.KafkaReceiverWordCount \--ma ...

  2. elasticsearch之hello(spring data整合)

    1.书写pom.xml文件 <dependencies> <dependency> <groupId>org.springframework.data</gr ...

  3. shell中与C语言中的区别

    shell中为啥与C语言有区别呢?弄成一样的不是很好么,其实不然,shell提供很多操作,这些操作不单单是执行程序或者命令,在很多时候是执行脚本的,简单的shell就是脚本编程,它的主要目的是处理文件 ...

  4. 分享Pos函数(比FastPos还要快)

    ): Integer; ): Integer; 主要用途是搜索字符串中第n个Substr. 经过测试,这2个函数的速度比直接用Pos+Copy快好几倍(如果字符串够长,可能10几倍) 比Pos+Del ...

  5. Runtime之方法

    前两篇介绍了类与对象.成员变量&属性&关联对象的相关知识,本篇我们将开始讲解Runtime中最有意思的一部分内容:消息处理机制.我们从一个示例开始. 在OC中,我们使用下面这种方式来调 ...

  6. nodejs从http升级到https(阿里云证书的使用)

    升级原因 1.各大搜索引擎中,https的网页的权重比一般的http的网页权重要高. 2.从用户体验的角度,一个老是被浏览器提醒该网页不可信的网页,总不会让用户感到安心 所以将网站从http升级为ht ...

  7. JavaScript操作和使用Cookie

    Cookie概述 Cookie是由服务器端生成并储存在浏览器客户端上的数据. 在javaweb开发中Cookie被当做java对象在web服务器端创建,并由web服务器发送给特定浏览器客户端,并且we ...

  8. Mybatis框架三:DAO层开发、Mapper动态代理开发

    这里是最基本的搭建:http://www.cnblogs.com/xuyiqing/p/8600888.html 接下来做到了简单的增删改查:http://www.cnblogs.com/xuyiqi ...

  9. 机器学习技法笔记:16 Finale

    Roadmap Feature Exploitation Techniques Error Optimization Techniques Overfitting Elimination Techni ...

  10. Spring Cloud Finchley 正式发布,包含 4 个重大更新!

    在 Spring 的官方博客上已经看到 Spring Cloud Finchley 在 06 月 19 日这一天正式发布了,我们在 Maven 中央仓库也看到了最新版的更新. Finchley 正式版 ...