码迷,mamicode.com
首页 > Web开发 > 详细

asp.net mvc之ActionResult

时间:2017-07-30 11:26:44      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:type   类型   cti   jpeg   encoding   服务   int   permanent   设置   

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

标签:type   类型   cti   jpeg   encoding   服务   int   permanent   设置   

原文地址:http://www.cnblogs.com/LittleFeiHu/p/7258167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!