mvc 返回 xml

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Web;
 7 using System.Web.Mvc;
 8 using System.Xml.Serialization;
 9 namespace PaiXie.Pos.Admin {
10         /// <summary>
11         /// 扩展System.Web.Mvc XmlRequestBehavior
12         /// 指定是否允许来自客户端的HTTP GET请求
13         ///</summary>
14         public enum XmlRequestBehavior {
15             /// <summary>
16             /// HTTP GET requests from the client are allowed.
17             /// 允许来自客户端的HTTP GET请求
18             /// </summary>      
19             AllowGet = 0,
20             /// <summary>
21             /// HTTP GET requests from the client are not allowed.
22             /// 不允许来自客户端的HTTP GET请求
23             /// </summary>
24             DenyGet = 1,
25         }
26         /// <summary>
27         /// 实现XmlResult继承ActionResult
28         /// 扩展MVC的ActionResult支持返回XML格式结果
29         /// </summary>
30         public class XmlResult : ActionResult {
31             /// <summary>
32             /// Initializes a new instance of the System.Web.Mvc.XmlResult class
33             /// 初始化
34             /// </summary>         
35             public XmlResult() { }
36             /// <summary>
37             /// Encoding
38             /// 编码格式
39             /// </summary>
40             public Encoding ContentEncoding { get; set; }
41             /// <summary>
42             /// Gets or sets the type of the content.
43             /// 获取或设置返回内容的类型
44             /// </summary>
45             public string ContentType { get; set; }
46             /// <summary>
47             /// Gets or sets the data
48             /// 获取或设置内容
49             /// </summary>
50             public object Data { get; set; }
51             /// <summary>
52             /// Gets or sets a value that indicates whether HTTP GET requests from the client
53             /// 获取或设置一个值,指示是否HTTP GET请求从客户端
54             /// </summary>
55             public XmlRequestBehavior XmlRequestBehavior { get; set; }
56             /// <summary>
57             /// Enables processing of the result of an action method by a custom type that
58             /// 处理结果
59             /// </summary>
60             /// <param name="context"></param>
61             public override void ExecuteResult(ControllerContext context) {
62                 if (context == null) { throw new ArgumentNullException("context"); }
63                 HttpRequestBase request = context.HttpContext.Request;
64                 if (XmlRequestBehavior == XmlRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
65                     throw new InvalidOperationException("XmlRequest_GetNotAllowed");
66                 }
67                 HttpResponseBase response = context.HttpContext.Response;
68                 response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/xml";
69                 if (this.ContentEncoding != null) {
70                     response.ContentEncoding = this.ContentEncoding;
71                 }
72                 if (Data != null) {
73                     using (MemoryStream ms = new MemoryStream()) {
74                         XmlSerializer xs = new XmlSerializer(Data.GetType());
75                         xs.Serialize(ms, Data); // 把数据序列化到内存流中                     
76                         ms.Position = 0;
77                         using (StreamReader sr = new StreamReader(ms)) {
78                             context.HttpContext.Response.Output.Write(sr.ReadToEnd()); // 输出流对象 
79                         }
80                     }
81                 }
82             }
83         }
84         /// <summary>
85         /// 扩展System.Mvc.Controller
86         /// </summary>
87         public static class ControllerExtension {
88             public static XmlResult Xml(this Controller request, object obj) { return Xml(obj, null, null, XmlRequestBehavior.DenyGet); }
89             public static XmlResult Xml(this Controller request, object obj, XmlRequestBehavior behavior) { return Xml(obj, null, null, behavior); }
90             public static XmlResult Xml(this Controller request, object obj, Encoding contentEncoding, XmlRequestBehavior behavior) { return Xml(obj, null, contentEncoding, behavior); }
91             public static XmlResult Xml(this Controller request, object obj, string contentType, Encoding contentEncoding, XmlRequestBehavior behavior) { return Xml(obj, contentType, contentEncoding, behavior); }
92             internal static XmlResult Xml(object data, string contentType, Encoding contentEncoding, XmlRequestBehavior behavior) { return new XmlResult() { ContentEncoding = contentEncoding, ContentType = contentType, Data = data, XmlRequestBehavior = behavior }; }
93         }    
94 }

测试

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 namespace PaiXie.Pos.Admin
 7 {
 8     /// <summary>
 9     /// mvc  返回 xml 扩展
10     /// </summary>
11     [AllowAnonymous]
12     [MvcMenuFilter(false)]
13     public class TestXmlController : Controller
14     {
15         // GET: /TestXml/GetActionResult?type=xml 
16         public ActionResult GetActionResult(string type) {
17             var data = new List<string>(); //注意,data必须是可被序列化的内容
18             data.Add("A");
19             data.Add("B");
20             data.Add("C");
21             a aa = new a();
22             aa.a1 = "001";
23             b bb = new b();
24             bb.b1 = "001001";
25             aa.a2 = bb;
26             if (type.ToLower() == "xml") {
27                 return this.Xml(aa, XmlRequestBehavior.AllowGet);
28             }
29             else if (type.ToLower() == "json") {
30                 return Json(aa, JsonRequestBehavior.AllowGet);
31             }
32             else {                  //error messages              
33                 return View("不支持此方法");
34             }
35         }
36 
37         public XmlResult GetXml() {
38             var data = new List<string>(); //注意,data必须是可被序列化的内容
39             data.Add("A");
40             data.Add("B");
41             data.Add("C");
42             return this.Xml(data, XmlRequestBehavior.AllowGet);
43         }
44     }
45     public class a {
46         public string a1 { get; set; }
47         public b a2 { get; set; }
48     }
49     public class b {
50         public string b1 { get; set; }
51     }
52 }
原文地址:https://www.cnblogs.com/lyl6796910/p/5213367.html