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. 【转载】我也说 IEnumerable,ICollection,IList,List之间的区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  2. azure之MSSQL服务性能测试

    azure给我们提供非常多的服务而这些服务可以让企业轻而易举地在上构建一个健壮的服务体系.但在使用azure的相关产品服务时还是应该对相关的服务有一些简单的性能了解才能更好的让企业购买适合自己的服务产 ...

  3. GoDaddy自动续费信用卡被扣款后的退款方法

    今天突然收到信用卡被GoDaddy捐款的通知,上GoDaddy网站上一看,是去年购买后来没有使用的一个CA证书被自动续费了.原来在GoDaddy购买的CA证书默认是每年自动续费的,这是一个大坑啊! 当 ...

  4. Hadoop HDFS 架构设计

    HDFS 简介 Hadoop Distributed File System,简称HDFS,是一个分布式文件系统. HDFS是高容错性的,可以部署在低成本的硬件之上,HDFS提供高吞吐量地对应用程序数 ...

  5. 使用jsdoc-toolkit实现JS API文档自动化

    在前面的博文中探讨自动化工程问题时,写过基于NodeJS的,使用gulp.grunt的jsdoc插件实现文档自动化.本文探讨基于java环境的自动化实现. 一.Java安装与环境配置 关于Java的安 ...

  6. C#与数据库访问技术总结(十一)之数据阅读器(DataReader)1

    数据阅读器 当执行返回结果集的命令时,需要一个方法从结果集中提取数据. 处理结果集的方法有两个: 第一,使用数据阅读器(DataReader): 第二,同时使用数据适配器(Data Adapter)和 ...

  7. paip. java resin 远程 调试 java resin remote debug

    paip. java resin 远程 调试 java resin remote debug 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 ...

  8. plsql无法连接64位oracle数据库的解决方法(图文解说)

    oracle11g下载页面:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html,找到适 ...

  9. MySql 分页

    MySql 分页 由于最近项目需要,于是就简单写了个分页查询.总体而言MySql 分页机制较为简单.数据库方面只需要使用limit即可实现分页.前后台交互就直接用session传了值. 下面就写写具体 ...

  10. canvas之万花筒

    canvas也有css3里transform的变换功能,transform的底层运算的方式是运用了线性代数里矩阵, 而矩阵是在我们的生活实践中会经常被使用,它可以把复杂的空间问题呈现出来,它还有很多实 ...