讓ASP.NET MVC頁面返回不同類型的內容

在ASP.NET MVC的controller中大部分方法返回的都是ActionResult,更確切的是ViewResult。它返回了一個View,一般情況下是一個HTML頁面。但是在某些情況下我們可能並不需要返回一個View,我們可能需要的是一個字符串,一個json或xml格式的文本,一個圖片。
ActionResult是一個抽象類,我們平時比較常用的ViewResult是它的派生類,所以我們也可以寫一個StringResult、XmlResult、ImageResult來實現上面提到的需求。由於返回字符串可以有更簡單的方法,直接將需要返回字符串的方法的返回值設置成string型就可以了,JsonResult在ASP.NET MVC中已經有提供。所以下面只演示XmlResult和ImageResult。
ASP.NET MVC項目是開源的(可以在http://www.codeplex.com/aspnet下載源代碼),所以我們可以參考其中ViewResult和JsonResult的代碼進行改寫。主要的思路是設置返回數據流HTTP Header中的Content-Type,然後將要返回的內容寫入Response中。


先演示XmlResult

XmlResult的代碼:

 

複製代碼
 1 public class XmlResult:ActionResult
 2     {
 3         // 可被序列化的內容
 4         object Data { getset; }
 5 
 6         // Data的類型
 7         Type DataType { getset; }
 8 
 9         // 構造器
10         public XmlResult(object data,Type type)
11         {
12             Data = data;
13             DataType = type;
14         }
15 
16         // 主要是重寫這個方法
17         public override void ExecuteResult(ControllerContext context)
18         {
19             if (context == null)
20             {
21                 throw new ArgumentNullException("context");
22             }
23 
24             HttpResponseBase response = context.HttpContext.Response;
25 
26             // 設置 HTTP Header 的 ContentType
27             response.ContentType = "text/xml";
28 
29             if (Data != null)
30             {
31                 // 序列化 Data 並寫入 Response
32                 XmlSerializer serializer = new XmlSerializer(DataType);
33                 MemoryStream ms = new MemoryStream();
34                 serializer.Serialize(ms,Data);
35                 response.Write(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
36             }
37         }
38     }
複製代碼

在controller中調用它

複製代碼
1 public ActionResult Xml()
2         {
3             // 創建一個DemoModal對象,No屬性爲1,Title屬性爲Test
4             DemoModal dm = new DemoModal() { No = 1, Title = "Test" };
5 
6             // 序列化爲XML格式顯示
7             XmlResult xResult = new XmlResult(dm, dm.GetType());
8             return xResult;
9         }
複製代碼

 

顯示出來的結果

 

下面演示的是ImageResult

ImageResult的代碼

複製代碼
 1 public class ImageResult:ActionResult
 2     {
 3         // 圖片
 4         public Image imageData;
 5 
 6         // 構造器
 7         public ImageResult(Image image)
 8         {
 9             imageData = image;
10         }
11 
12         // 主要需要重寫的方法
13         public override void ExecuteResult(ControllerContext context)
14         {
15             if (context == null)
16             {
17                 throw new ArgumentNullException("context");
18             }
19 
20             HttpResponseBase response = context.HttpContext.Response;
21 
22             // 設置 HTTP Header
23             response.ContentType = "image/jpeg";
24 
25             // 將圖片數據寫入Response
26             imageData.Save(context.HttpContext.Response.OutputStream, ImageFormat.Jpeg);
27         }
28     }
複製代碼

在controller中調用

 

複製代碼
 1 public ActionResult Img()
 2         {
 3             // 獲取博客園空間頂部的banner圖片
 4             WebRequest req = WebRequest.Create("http://space.cnblogs.com/images/a4/banner.jpg");
 5             WebResponse res = req.GetResponse();
 6             Stream resStream = res.GetResponseStream();
 7             Image img = Image.FromStream(resStream);
 8 
 9             // 輸出給客戶端
10             ImageResult r = new ImageResult(img);
11             return r;
12         }
複製代碼

結果圖

這個比較多用在向客戶端傳送驗證碼圖片時。
(轉:http://www.cnblogs.com/Snowdreams/archive/2008/11/15/let-aspnet-mvc-view-return-different-type-content.html)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章