文章出處

我們都知道Asp.net MVC自帶的Action可以有多種類型,比如ActionResult,ContentResult,JsonResult……,但是很遺憾沒有支持直接返回XML的XmlResult。

當然,你也可以用ActionResult或者ContentResult,然后直接返回xml字符串。

如果我們想要想JsonResult一樣來調用和返回xml結果,我們可以自己新建擴展XmlResult,該怎么辦呢?不多說,看下面實例:

 

第一步,擴展System.Web.Mvc XmlRequestBehavior

/// <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,
}

 

第二步,實現XmlResult繼承ActionResult

/// <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()); // 輸出流對象 
                }
            }
        }
    }
}

 

注釋:如果想要修改反序列化后的xml命名空間,可以使用自定義命名空間:

//使用你的定義的命名空間名稱
var ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://api.xxxx.com/1.0/");

然后這里添加自定義命名空間,注意第三個參數
xs.Serialize(ms, Data ,ns); // 把數據序列化到內存流中 

 

 

第三步,擴展System.Mvc.Controller

/// <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 }; }
}

 

到此就完成啦,下面就看看怎么調用:

第四步,在Controller里調用XmlResult

public ActionResult GetActionResult(string type)
{
    var data = new List<string>(); //注意,data必須是可被序列化的內容
    data.Add("A");
    data.Add("B");
    data.Add("C");

    if (type.ToLower() == "xml")
    {
        return this.Xml(data, XmlRequestBehavior.AllowGet);
    }
    else if (type.ToLower() == "json")
    {
        return Json(data, 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);
}

運行一下,看看實際返回結果吧!

上面的示例運行發揮的結果是這樣:

<?xml version="1.0"?>
<ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<string>A</string>
<string>B</string>
<string>C</string>
</ArrayOfString>

 

以下是完整的代碼:

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml.Serialization;

namespace Onexz.API.Models
{
    /// <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 }; }
    }

}

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()