asp.net mvc之ActionResult
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的更多相关文章
- [转载]深入理解ASP.NET MVC之ActionResult
Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...
- Asp.net MVC 之 ActionResult
Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...
- ASP.NET MVC自定义ActionResult实现文件压缩
有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载 using System; using System.Collections; usi ...
- ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出
之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itexts ...
- ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别
如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult
- Asp.net MVC 控制器ActionResult的例子
ActionResult 父类型 ViewResult View() 多重载应用 PartialViewResult PartialView() 部分试图 New EmptyResult() 空 如 ...
- Asp.net MVC 之ActionResult
ActionResult 派生出以下子类: ViewResult 返回一个网页视图 PartialViewResult 返回一个网页视图,但不适用布局页. ContentResult 返回一段字符串文 ...
- ASP.NET MVC中ActionResult的不同返回方式
1.返回视图 return View();//返回方法名对应的视图 return View("aaa");//返回名称为aaa的视图 2.返回文本内容 return Content ...
- 【转】ASP.NET MVC学习笔记-Controller的ActionResult
1. 返回ViewResult public ActionResult Index() { ViewData["Message"] = "Welcome ...
随机推荐
- kali安装配置ftp
参考:https://zhidao.baidu.com/question/1511146077646448900.html 一)安装 1.用sudo apt-get install 下载安装包
- Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore
原文出处: 海子 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅 ...
- The First BoKe
A.如何看待师生关系 说起师生关系,我们每个人都有不同的见解,但无一例外,师者,传道授业解惑也,老师的为学生传授的才能知识,是学生今后发展的宝贵财富,无论是从哪方面,都可以对学生起着积极向上的作用.而 ...
- Android studio 中的TabWidget
1.TabWidget 的 layout文件 <?xml version="1.0" encoding="utf-8"?> <TabHost ...
- Tomcat 在 Linux 上的安装和配置
一.文件上传 先上传tomcat安装文件到Linux服务器 二.解压安装 使用以下命令解压安装包 .tar.gz 解压成功会生成一个文件夹 tomcat服务器运行时是需要JDK支持的,所以必须先安装好 ...
- HTTP 协议基础概念和报文结构
基础概念 1.WWW(World Wide Web,万维网)构建技术有3项: (1)把SGML(Standard Generalized Markup Language,标准通用标记语言)作为页面的文 ...
- Appium移动自动化测试入门及简单实例(python)
1.介绍 Appium是一个移动端的自动化框架,可用于测试原生应用.移动网页应用和混合型应用,且是跨平台的.可用于IOS和Android以及firefox的操作系统.原生的应用是指用android或i ...
- Spring.Net实现跨数据库服务层事务管理
在实际项目中用了Spring.Net框架之后,发现这框架在处理数据库事务上特别强大,声明式的数据库事务解放了一般开发人员对数据库事务操作的复杂处理.尤其是多数据库事务处理上,尤为显得更简便,几个简单的 ...
- Maven - 实例-4-依赖传递
这里以Eclipse创建Maven工程来演示. Setp-1 创建Maven项目 File ---> New ---> Maven Project ---> 默认勾选"Us ...
- LeetCode--No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...