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数据,之后 ...
随机推荐
- 拉格朗日乘子法和KKT条件
拉格朗日乘子法(Lagrange Multiplier)和KKT(Karush-Kuhn-Tucker)条件是求解约束优化问题的重要方法,在有等式约束时使用拉格朗日乘子法,在有不等约束时使用KKT条件 ...
- C++ 类的静态成员详细讲解
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节 ...
- Shiro-多Realm验证
1.多Realm验证 存在这样一种场景,同一个密码可能在MqSQL中存储,也可能在Oracle中存储,有可能MqSQL中使用的是MD5加密算法,而Oracle使用SHA1加密算法.这就需要有多个Rea ...
- 【Leetcode】【Hard】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- Server Develop (五) Linux并发模型
Linux并发模型 目前可以实现并发程序的方法有Apache模型(Process Per Connection,简称PPC),TPC(Thread PerConnection)模型,以及select模 ...
- [异常] VC6.0 error LNK2001: unresolved external symbol _main解决办法
来自:http://www.douban.com/note/65638800/ 学习VC++时经常会遇到链接错误LNK2001,该错误非常讨厌,因为对于编程者来说,最好改的错误莫过于编译错误,而一般说 ...
- CheckStyle, 强制你遵循编码规范
如今代码静态检查越来越重要,已经成为构建高质量软件的不可或缺的一个验证步骤.如果你使用的是java语言,那么CheckStyle则是一个利器. CheckStyle能够帮助程序员检查代码是否符合制定的 ...
- [公告]这里的博客将不再更新,最新博客请移步至blog.coderzh.com
公告:我的博客已迁移至独立博客:http://blog.coderzh.com/ 感谢大家支持!同时欢迎关注我的微信公众号:hacker-thinking <---- 扫描左侧二维码关注
- 利用nodejs模块缓存机制创建“全局变量”
在<深入浅出nodejs>有这样一段(有部分增减): 1.nodejs引入模块分四个步骤 路径分析 文件定位 编译执行 加入内存 2.核心模块部分在node源代码的编译过程中就编译成了二级 ...
- 使用TabBarController(代码实现)
step01:使用Xcode创建一个项目 step02:填写项目必要信息 step03:检查文件结构树是否正确 step04:创建一些类,这些类将会在后面用到!(选择Swift File) step0 ...