.NET MVC中的ActionResult(一)

0   摘要

  本文介绍了ASP.NET MVC中的ActionResult,本节主要介绍 EmptyResult / Content Result /JavaScriptResult /JsonResult /FileResult (FileContentResult ,FileStreamResult 和FilePathResult)。

1、ActionResult是什么

    对于来自客户端的访问请求,最终的处理体现在针对目标Action方法的执行,我们可以在定义Action方法的时候人为地控制对请求的响应。抽象类Controller具有一个只读的Response属性表示当前的HttpResponse,我们可以直接利用它来实现对请求的响应。

   ActionInvoker 调用Action,创建相应的ActionResult对象,ActionResult是对Action执行结果的封装。ActionResult就是执行Action的结果。ActionResult是一个抽象类型,最终的请求响应实现在抽象方法ExecuteResult方法中。

     public abstract class ActionResult
        {
            //其他成员
            public abstract void ExecuteResult(ControllerContext context);
        }

2、常用的ActionResult

2.1   EmptyResult  &  ContentResult(简单)

  如果Action的执行结果是void 或者 null,ActionInvoker会创建一个EmptyResult,我们也可以自己返回EmptyResult。当我们返回一个字符串时可以设置返回值为ContentResult类型。

 //1、Empty
        public EmptyResult GetEmpty()
        {
            //返回null或者一个空结果
            return null;
            //return new EmptyResult();
        }
 //2、Content
        public ContentResult GetContent()
        {
            return Content("返回了一个字符串");
        }

 2.2  JavaScriptResult  &  JsonResult(常用于Ajax请求的)

2.2.1  JsonResult 

   JsonResult 的ExecuteResult方法中通过JavaScriptSerializer对数据对象的序列化,并将序列化生成的JSON字符串作为内容对请求进行响应。

 public JsonResult GetJson()
        {
            List<Object> userList = new List<object>()
            {
                  new {name="zs",age=20},
                  new {name="ls",age=21},
                  new {name="ww",age=22}
            };
            //JsonResult的ExecuteResult方法使用JavaScriptSerializer序列化对象
       //下边的序列化操作不用写,mvc会自动帮我们进行序列化,当json数据很大时使用Json.net进行序列化 JavaScriptSerializer serializer = new JavaScriptSerializer(); string userListStr=serializer.Serialize(userList);return Json(new { msg = "ok", userList= userListStr}, JsonRequestBehavior.AllowGet); }

2.2.2  JavaScriptResult

   JavaScriptResult 使我们可以在服务端动态地生成一段字符串类型的JavaScript脚本,并以此作为请求的响应。我们也可以通过指定ContentResult的媒体类型来使用Content实现和JavaScriptResult同样的效果。

 1    //返回Js脚本
 2         UserInfo user = new UserInfo { Id = 1, Name = "刘备", Age = 22 };
 3         public JavaScriptResult GetJS()
 4         {
 5             string js =string.Format("alert('hello:{0}')", user.Name);
 6             return JavaScript(js);
 7         }
 8 
 9         public ContentResult GetJS2()
10         {
11             string js = string.Format("alert('hello:{0}')", user.Name);
12             return Content(js, "application/x-javascript");
13         }

2.3  FileResult(文件类型)

  ASP.NET MVC定义了三个具体的FileResult,分别是 FileContentResult、FilePathResult和FileStreamResult,这三个子类实现FileResult的WriteFile方式不同 。

  针对文件的响应具有两种形式,即 内联(Inline) 和 附件(Attachment) 。一般来说,内联会利用浏览器直接打开响应的文件,而附件会以独立的文件下载到客户端。文件响应在默认情况下采用内联的方式,如果需要采用附件的形式,需要为响应创建一个名称为Content-Disposition的报头,该报头值的格式为“attachment; filename={ FileDownloadName }”,我们一般会为下载的文件指定一个文件名,这个文件名可以通过FileResult的FileDownloadName属性来指定。

2.3.1 FileContentResult

  实现原理:  调用当前HttpResponse的OutputStream属性的Write方法直接将表示文件内容的字节数组写入响应输出流 。

  使用场景:由于FileContentResult是根据字节数组创建的,当我们需要动态生成响应文件内容(而不是从物理文件中读取)时,FileContentResult是一个不错的选择。

 //实现FileContent类的WriteFile方法
   protected override void WriteFile(HttpResponseBase response)
    {
        response.OutputStream.Write(this.FileContents, 0, this.FileContents.Length);
    }

2.3.2 FilePathResult

  实现原理:根据物理文件路径来创建FileResult。如下面的代码片断所示,表示响应文件的路径通过只读属性FileName表示,该属性在构造函数中被初始化。在实现的WriteFile方法中,FilePathResult直接将文件路径作为参数调用当前HttpResponse的TransmitFile实现了针对文件内容的响应。抽象类Controller同样定义了两个File方法重载来根据文件路径创建相应的FilePathResult。

 //实现FileContent类的WriteFile方法
    protected override void WriteFile(HttpResponseBase response)
    {
        response.TransmitFile(this.FileName);
    }

2.3.3 FileStreamResult

  实现原理:FileStreamResult允许我们通过一个用于读取文件内容的流来创建FileResult。如下面的代码片断所示,读取文件流通过只读属性FileStream表示,该属性在构造函数中被初始化。在实现的WriteFile方法中,FileStreamResult通过指定的文件流读取文件内容,并最终调用当前HttpResponse的OutputStream属性的Write方法将读取的内容写入当前HTTP响应的输出流中。

 1 public class FileStreamResult : FileResult
 2 {
 3     public Stream FileStream { get; }
 4     public FileStreamResult(Stream fileStream, string contentType);
 5     protected override void WriteFile(HttpResponseBase response)
 6     {
 7         Stream outputStream = response.OutputStream;
 8         using (this.FileStream)
 9         {
10             byte[] buffer = new byte[0x1000];
11             while (true)
12             {
13                 int count = this.FileStream.Read(buffer, 0, 0x1000);
14                 if (count == 0)
15                 {
16                     return;
17                 }
18                 outputStream.Write(buffer, 0, count);
19             }
20         }
21     }
22 }

2.3.4 一个简单的返回图片案例

Controller中的代码:

 1  public class ImageController : Controller
 2     {
 3         // GET: Image
 4         public ActionResult Index()
 5         {
 6             return View();
 7         }
 8         public ActionResult GetImage(bool isDownLoad)
 9         {
10             string path = Server.MapPath("/Images/aaa.png");
11            
12             if (!isDownLoad)
13             {
14                 //内联形式,在浏览器打开文件
15                 return File(path, "image/jpeg");
16             }
17             else
18             {
19                 //附件形式,下载文件名为“我的下载.png”
20                 return File(path, "image/jpeg", "我的下载.png");
21             }         
22         }
23     }

View中的代码:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>展示图片</h2>
<img src="@Url.Action("GetImage", "Image", new { isDownLoad = true })"/>

 

猜你喜欢

转载自www.cnblogs.com/wyy1234/p/9259087.html