mvc 返回 xml
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的更多相关文章
- Spring MVC 返回 xml json pdf 数据的配置方法
		
<!-- Spring MVC 返回 xml 数据的配置方法 --> <bean class="org.springframework.web.servlet.vi ...
 - Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据
		
我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...
 - spring mvc 返回xml格式数据
		
1.问题 : 因为业务需要,需要发送xml格式的数据,使用spring mvc 自己解析,就不用费心去自己搞这些东西. 2.解决: 新建一个实体类,直接在实体类中添加注解即可,如下: @XmlRoot ...
 - web api .net  C#  mvc API返回XML文档的解析并取值
		
[HttpGet] public System.Net.Http.HttpResponseMessage GetNotify() { var xmlstring = @" <xml&g ...
 - spring mvc返回json字符串的方式
		
spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json 优点:不需要自己再处理 步骤一:在spring- ...
 - springmvc 返回xml
		
需求: 1.springmvc返回xml: 技术及环境: Spring 4.3.1.RELEASE JDK 1.8 IDEA 15.0.6 Maven 3 实现: spirngxml的配置主要如下: ...
 - 使用spring mvc返回JSON,chrome可以,firefox不行的问题定位
		
转载http://ks.netease.com/blog?id=4024 作者:李景 场景: 前端Post请求同一个url地址,在chrome浏览器上有正常返回json,而在 ...
 - 【.net 深呼吸】聊聊WCF服务返回XML或JSON格式数据
		
有时候,为了让数据可以“跨国经营”,尤其是HTTP Web有关的东东,会将数据内容以 XML 或 JSON 的格式返回,这样一来,不管客户端平台是四大文明古国,还是处于蒙昧时代的原始部落,都可以使用这 ...
 - mvc 返回值
		
mvc返回值为Model类型 public ActionResult Index(T result) { return View(result); } view中的对象即为页面中的Model数据,之后 ...
 
随机推荐
- UVA 11732 strcmp() Anyone? (压缩版字典树)
			
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
 - IIS Connection Timeout vs httpRuntime executionTimeout
			
IIS Connection Timeout specifies how long, in seconds, should the code wait before timing out from t ...
 - SVM 简要推导过程
			
SVM 是一块很大的内容,网上有写得非常精彩的博客.这篇博客目的不是详细阐述每一个理论和细节,而在于在不丢失重要推导步骤的条件下从宏观上把握 SVM 的思路. 1. 问题由来 SVM (支持向量机) ...
 - HTTPD服务 openssl的https服务机制
			
环境: 环境: httpd服务器:10.140.165.169 CA服务器:10.140.165.93 CA服务器配置: 1.安装openssl [root@cnhzdhcp16593 ~]# yum ...
 - Unrecognized Windows Sockets error: 0: JVM_Bind     异常怎么办
			
Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法 java.net.SocketException: Unrecognized Window ...
 - .NET Socket服务编程之-高效连接接入编
			
在.NET上编写网络服务深入都有2,3年了,而这些时间时如何在.NET里实现网络服务积累了一些经验.在接下来的时间里会把这方面的经验通过博客的方式分享出来.而这一章主要是讲解在如果提高服务连接接入的效 ...
 - Dynamic CRM 2013学习笔记(五)禁止修改、删除审批通过后的单据
			
审批通过后的单据,一般要对其进行控制,不能修改,不能添加,删除等,下面分别介绍下如何实现: 一. 禁止修改: 1. 主表控制,如果页面上审批状态为审批中或审批通过,就把整个页面都disable掉 1: ...
 - NBIbatis 微信框架
			
微信框架 必须先完成NBIbatis基础框架的[框架配置],本项目才能正常运行. 漂亮会议展示 这是一套漂亮的会议展示完整界面/ ForePages/ HomePage_1210.htm Wechat ...
 - [WinAPI] API 6 [操作驱动器挂载点]
			
驱动器挂载点,又可以称作卷挂载点.挂载点实际上是操作系统或者用户设置的,用来进入一个逻辑驱动器或者卷的入口.在设置了卷的挂载点后,用户或者应用程序可以使用卷标或者指定的挂载点来进入卷.比如在“C:\” ...
 - servlet 读取web.xml参数
			
1初始化参数init-param init-param是配置在web.xml的<servlet>标签里的,也就是说,是归该servlet单独所有的. 实例 <servlet> ...