扩展Controller实现XmlResult以返回XML格式数据
1. 扩展System.Web.Mvc XmlRequestBehavior
namespace System.Web.Mvc
{
public enum XmlRequestBehavior
{
// Summary:
// HTTP GET requests from the client are allowed.
AllowGet = 0,
//
// Summary:
// HTTP GET requests from the client are not allowed.
DenyGet = 1,
}
}
2. 实现XmlResult继承ActionResult
namespace System.Web.Mvc
{
public class XmlResult:ActionResult
{
// Summary:
// Initializes a new instance of the System.Web.Mvc.JsonResult class.
public XmlResult() { }
public Encoding ContentEncoding { get; set; }
//
// Summary:
// Gets or sets the type of the content.
//
// Returns:
// The type of the content.
public string ContentType { get; set; }
//
// Summary:
// Gets or sets the data.
//
// Returns:
// The data.
public object Data { get; set; }
//
// Summary:
// Gets or sets a value that indicates whether HTTP GET requests from the client
// are allowed.
//
// Returns:
// A value that indicates whether HTTP GET requests from the client are allowed.
public XmlRequestBehavior XmlRequestBehavior { get; set; }
// Summary:
// Enables processing of the result of an action method by a custom type that
// inherits from the System.Web.Mvc.ActionResult class.
//
// Parameters:
// context:
// The context within which the result is executed.
//
// Exceptions:
// System.ArgumentNullException:
// The context parameter is null.
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());
}
}
}
}
}
}
3. 扩展System.Mvc.Controller
namespace System.Web.Mvc
{
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);
}
internal static XmlResult Xml(object data, string contentType, Encoding contentEncoding, XmlRequestBehavior behavior)
{
return new XmlResult()
{
ContentEncoding = contentEncoding,
ContentType = contentType,
Data = data,
XmlRequestBehavior = behavior
};
}
}
}
4 Controller调用
public ActionResult Index()
{
return View();
}
public ActionResult GetActionResult(string type)
{
var data = GetData();
if (type.ToLower() == "xml")
{
return this.Xml(null, XmlRequestBehavior.AllowGet);
}
else if (type.ToLower() == "json")
{
return Json(data, JsonRequestBehavior.AllowGet);
}
else
{
//error messages
return View("不支持此方法");
}
}
public XmlResult GetXml()
{
var data = GetData();
return this.Xml(data,XmlRequestBehavior.AllowGet);
}
public JsonResult GetJson()
{
var data = GetData();
return Json(data, JsonRequestBehavior.AllowGet);
}
public List<User> GetData()
{
List<User> users = new List<User>();
users.Add(new User("sikai", "cao", '1', new DateTime(1984, 4, 4)));
users.Add(new User("sikai", "cao", '1', new DateTime(1984, 4, 5)));
users.Add(new User("sikai", "cao", '1', new DateTime(1984, 4, 6)));
users.Add(new User("sikai", "cao", '1', new DateTime(1984, 4, 7)));
return users;
}

浙公网安备 33010602011771号