mvc 返回 xml
2016-02-24 16:13
246 查看
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using System.Xml.Serialization; namespace PaiXie.Pos.Admin { /// <summary> /// 扩展System.Web.Mvc XmlRequestBehavior /// 指定是否允许来自客户端的HTTP GET请求 ///</summary> public enum XmlRequestBehavior { /// <summary> /// HTTP GET requests from the client are allowed. /// 允许来自客户端的HTTP GET请求 /// </summary> AllowGet = 0, /// <summary> /// HTTP GET requests from the client are not allowed. /// 不允许来自客户端的HTTP GET请求 /// </summary> DenyGet = 1, } /// <summary> /// 实现XmlResult继承ActionResult /// 扩展MVC的ActionResult支持返回XML格式结果 /// </summary> public class XmlResult : ActionResult { /// <summary> /// Initializes a new instance of the System.Web.Mvc.XmlResult class /// 初始化 /// </summary> public XmlResult() { } /// <summary> /// Encoding /// 编码格式 /// </summary> public Encoding ContentEncoding { get; set; } /// <summary> /// Gets or sets the type of the content. /// 获取或设置返回内容的类型 /// </summary> public string ContentType { get; set; } /// <summary> /// Gets or sets the data /// 获取或设置内容 /// </summary> public object Data { get; set; } /// <summary> /// Gets or sets a value that indicates whether HTTP GET requests from the client /// 获取或设置一个值,指示是否HTTP GET请求从客户端 /// </summary> public XmlRequestBehavior XmlRequestBehavior { get; set; } /// <summary> /// Enables processing of the result of an action method by a custom type that /// 处理结果 /// </summary> /// <param name="context"></param> public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } HttpRequestBase request = context.HttpContext.Request; if (XmlRequestBehavior == XmlRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("XmlRequest_GetNotAllowed"); } HttpResponseBase response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/xml"; if (this.ContentEncoding != null) { response.ContentEncoding = this.ContentEncoding; } if (Data != null) { using (MemoryStream ms = new MemoryStream()) { XmlSerializer xs = new XmlSerializer(Data.GetType()); xs.Serialize(ms, Data); // 把数据序列化到内存流中 ms.Position = 0; using (StreamReader sr = new StreamReader(ms)) { context.HttpContext.Response.Output.Write(sr.ReadToEnd()); // 输出流对象 } } } } } /// <summary> /// 扩展System.Mvc.Controller /// </summary> public static class ControllerExtension { public static XmlResult Xml(this Controller request, object obj) { return Xml(obj, null, null, XmlRequestBehavior.DenyGet); } public static XmlResult Xml(this Controller request, object obj, XmlRequestBehavior behavior) { return Xml(obj, null, null, behavior); } public static XmlResult Xml(this Controller request, object obj, Encoding contentEncoding, XmlRequestBehavior behavior) { return Xml(obj, null, contentEncoding, behavior); } public static XmlResult Xml(this Controller request, object obj, string contentType, Encoding contentEncoding, XmlRequestBehavior behavior) { return Xml(obj, contentType, contentEncoding, behavior); } internal static XmlResult Xml(object data, string contentType, Encoding contentEncoding, XmlRequestBehavior behavior) { return new XmlResult() { ContentEncoding = contentEncoding, ContentType = contentType, Data = data, XmlRequestBehavior = behavior }; } } }
测试
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace PaiXie.Pos.Admin { /// <summary> /// mvc 返回 xml 扩展 /// </summary> [AllowAnonymous] [MvcMenuFilter(false)] public class TestXmlController : Controller { // GET: /TestXml/GetActionResult?type=xml public ActionResult GetActionResult(string type) { var data = new List<string>(); //注意,data必须是可被序列化的内容 data.Add("A"); data.Add("B"); data.Add("C"); a aa = new a(); aa.a1 = "001"; b bb = new b(); bb.b1 = "001001"; aa.a2 = bb; if (type.ToLower() == "xml") { return this.Xml(aa, XmlRequestBehavior.AllowGet); } else if (type.ToLower() == "json") { return Json(aa, JsonRequestBehavior.AllowGet); } else { //error messages return View("不支持此方法"); } } public XmlResult GetXml() { var data = new List<string>(); //注意,data必须是可被序列化的内容 data.Add("A"); data.Add("B"); data.Add("C"); return this.Xml(data, XmlRequestBehavior.AllowGet); } } public class a { public string a1 { get; set; } public b a2 { get; set; } } public class b { public string b1 { get; set; } } }
相关文章推荐
- 深入理解Java的接口和抽象类
- Animation动画详解(六)——ValueAnimator高级进阶(二)
- svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法
- 无序数组 的特殊排序
- 移动应用通过umeng实现第三方登录--已微信为例
- SignApk.java 源码
- JqueryMobile地址选择显示问题
- 如何删除XCode里的证书
- 2016年第4本:大系统观,修订1.0.1版本
- spring InitializingBean接口
- http协议详解
- 经典的概率算法
- mvc 权限验证
- 2、构造、析构和赋值运算
- iOS 编码复习(二)—— runtime
- 程序运行时的内存空间分布
- linux查看网卡流量工具lftop命令
- CODEVS 1954 线段树
- SDWebImage原理
- android apk的编译过程