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 = ,
/// <summary>
/// HTTP GET requests from the client are not allowed.
/// 不允许来自客户端的HTTP GET请求
/// </summary>
DenyGet = ,
}
/// <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 = ;
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 = "";
b bb = new b();
bb.b1 = "";
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; }
}
}

mvc 返回 xml的更多相关文章

  1. Spring MVC 返回 xml json pdf 数据的配置方法

    <!-- Spring MVC 返回 xml 数据的配置方法 -->     <bean class="org.springframework.web.servlet.vi ...

  2. Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据

    我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...

  3. spring mvc 返回xml格式数据

    1.问题 : 因为业务需要,需要发送xml格式的数据,使用spring mvc 自己解析,就不用费心去自己搞这些东西. 2.解决: 新建一个实体类,直接在实体类中添加注解即可,如下: @XmlRoot ...

  4. web api .net C# mvc API返回XML文档的解析并取值

    [HttpGet] public System.Net.Http.HttpResponseMessage GetNotify() { var xmlstring = @" <xml&g ...

  5. spring mvc返回json字符串的方式

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  6. springmvc 返回xml

    需求: 1.springmvc返回xml: 技术及环境: Spring 4.3.1.RELEASE JDK 1.8 IDEA 15.0.6 Maven 3 实现: spirngxml的配置主要如下: ...

  7. 使用spring mvc返回JSON,chrome可以,firefox不行的问题定位

    转载http://ks.netease.com/blog?id=4024 作者:李景     场景:          前端Post请求同一个url地址,在chrome浏览器上有正常返回json,而在 ...

  8. 【.net 深呼吸】聊聊WCF服务返回XML或JSON格式数据

    有时候,为了让数据可以“跨国经营”,尤其是HTTP Web有关的东东,会将数据内容以 XML 或 JSON 的格式返回,这样一来,不管客户端平台是四大文明古国,还是处于蒙昧时代的原始部落,都可以使用这 ...

  9. mvc 返回值

    mvc返回值为Model类型 public ActionResult Index(T result) { return View(result); } view中的对象即为页面中的Model数据,之后 ...

随机推荐

  1. linux tcp协议状态机

    截图来自百度文库 TCP状态-有限状态机

  2. Hibernate关联关系配置(一对多、一对一和多对多)

    第一种关联关系:一对多(多对一) "一对多"是最普遍的映射关系,简单来讲就如消费者与订单的关系. 一对多:从消费者角的度来说一个消费者可以有多个订单,即为一对多. 多对一:从订单的 ...

  3. extjs中form.reset(true)出现的bug修复

    在之前的开发extjs中,用ext.form.panel开发了一个表单,当使用了reset(true)之后,再次使用getRecord()却还是可以得到值,该值为上一次的旧值,查看了api文档,再结合 ...

  4. 使用lock和condition实现的阻塞队列-字符串

    在jdk 的API中提供了一个字符串的阻塞队列 : class BoundedBuffer { final Lock lock = new ReentrantLock(); final Conditi ...

  5. 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化

    谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...

  6. Lambda表达式的前世今生

    Lambda 表达式 早在 C# 1.0 时,C#中就引入了委托(delegate)类型的概念.通过使用这个类型,我们可以将函数作为参数进行传递.在某种意义上,委托可理解为一种托管的强类型的函数指针. ...

  7. 2014年团队博客TOP10

    2014年通过这个团队博客,葡萄城共输出了51篇原创技术博客(含翻译),总阅读超过9万人次,约有1万人次是通过RSS订阅方式阅读,总评论超过500人次. 这里我们通过阅读排序,选出2014年团队博客T ...

  8. [Java拾遗五]使用Session防止表单重复提交

    申明:此文章属于转载, 转自博客: http://www.cnblogs.com/xdp-gacl/p/3859416.html在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没 ...

  9. ftp 操作,支持断点续传或者继续下载。

    1.ftpclient 类 public class FTPClient:IDisposable { public static object _obj = new object(); #region ...

  10. [转] java.lang.IllegalArgumentException: Document base D:\apache-tomcat-7.0.47\webapps\XXX错误

    启动服务器,报上述错误,是因为在服务器中(如tomcat)删除了某项目,单残留的配置文件没删除造成的.解决办法是到服务器中找到conf\Catalina\localhost下找到错误信息中对应的配置文 ...