using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; namespace Pcb.Common { public class StringResult : ActionResult { // 可被序列化的内容 public object Data { get; set; } // Data的类型 public Type DataType { get; set; } public string ContentType { get; set; } // 构造器 public StringResult() { } // 构造器 public StringResult(object data, Type type) { Data = data; DataType = type; } // 主要是重写这个方法 public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } HttpResponseBase response = context.HttpContext.Response; // 设置 HTTP Header 的 ContentType if (string.IsNullOrEmpty(ContentType)) ContentType="text/plain"; response.ContentType = ContentType; if (Data != null) { response.Write(Data.ToString()); } } } }