您的位置:首页 > 其它

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; }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: