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

当然,你也可以用ActionResult或者ContentResult,然后直接返回xml字符串。

如果我们想要想JsonResult一样来调用和返回xml结果,我们可以自己新建扩展XmlResult,该怎么办呢?不多说,看下面实例:

第一步,扩展System.Web.Mvc XmlRequestBehavior

/// <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 = ,
}

第二步,实现XmlResult继承ActionResult

/// <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()); // 输出流对象
}
}
}
}
}

注释:如果想要修改反序列化后的xml命名空间,可以使用自定义命名空间:

//使用你的定义的命名空间名称
var ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://api.xxxx.com/1.0/"); 然后这里添加自定义命名空间,注意第三个参数
xs.Serialize(ms, Data ,ns); // 把数据序列化到内存流中

第三步,扩展System.Mvc.Controller

/// <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 }; }
}

到此就完成啦,下面就看看怎么调用:

第四步,在Controller里调用XmlResult

public ActionResult GetActionResult(string type)
{
var data = new List<string>(); //注意,data必须是可被序列化的内容
data.Add("A");
data.Add("B");
data.Add("C"); if (type.ToLower() == "xml")
{
return this.Xml(data, XmlRequestBehavior.AllowGet);
}
else if (type.ToLower() == "json")
{
return Json(data, 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);
}

运行一下,看看实际返回结果吧!

上面的示例运行发挥的结果是这样:

<?xml version="1.0"?>
<ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<string>A</string>
<string>B</string>
<string>C</string>
</ArrayOfString>

以下是完整的代码:

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml.Serialization; namespace Onexz.API.Models
{
/// <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 }; }
} }

其他

常见的各种Result都是 ActionResult 派生的,都去各自实现 自己的 成员 及 核心过程: ExecuteResult
比如以下是JsonResult的部分源代码,我们可以看到它在核心 函数 ExecuteResult 做了头部处理,做了Http Body 内容处理:

public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
if (this.ContentEncoding != null)
response.ContentEncoding = this.ContentEncoding;
if (this.Data == null)
return;
JavaScriptSerializer scriptSerializer = new JavaScriptSerializer();
if (this.MaxJsonLength.HasValue)
scriptSerializer.MaxJsonLength = this.MaxJsonLength.Value;
if (this.RecursionLimit.HasValue)
scriptSerializer.RecursionLimit = this.RecursionLimit.Value;
response.Write(scriptSerializer.Serialize(this.Data));
}

所以以此类推,如果你想做其他result,只需要根据result最终的样子  —— 核心通过派生ActionResult,并重写ExecuteResult 即可实现。

public class ImageResult : ActionResult
{
public ImageResult() { }
public Image Image { get; set; }
public ImageFormat ImageFormat { get; set; }
public override void ExecuteResult(ControllerContext context)
{
// verify properties
if (Image == null)
{
throw new ArgumentNullException("Image");
}
if (ImageFormat == null)
{
throw new ArgumentNullException("ImageFormat");
}
// output
context.HttpContext.Response.Clear();
if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";
Image.Save(context.HttpContext.Response.OutputStream, ImageFormat);
}
}

【完】

Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据的更多相关文章

  1. ASP.NET MVC学前篇之扩展方法、链式编程

    ASP.NET MVC学前篇之扩展方法.链式编程 前言 目的没有别的,就是介绍几点在ASP.NETMVC 用到C#语言特性,还有一些其他琐碎的知识点,强行的划分一个范围的话,只能说都跟MVC有关,有的 ...

  2. [ASP.NET MVC 小牛之路]10 - Controller 和 Action (2)

    继上一篇文章之后,本文将介绍 Controller 和 Action 的一些较高级特性,包括 Controller Factory.Action Invoker 和异步 Controller 等内容. ...

  3. Asp.Net MVC以 JSON传值扩展方法

    Asp.Net在客户端和服务器端,以JSON形式相互传值,可写扩展方法,用到的类型如下: DataContractJsonSerializer类: 该类在System.Runtime.Serializ ...

  4. Asp.Net MVC以JSON传值扩展方法

    Asp.Net在客户端和服务器端,以JSON形式相互传值,可写扩展方法,用到的类型如下: DataContractJsonSerializer类: 该类在System.Runtime.Serializ ...

  5. ASP.NET MVC 5 Web编程3 -- Controller的应用及扩展

    Controller基础 一. 访问修饰符 1.1 类的访问修饰符 Controller类的访问修饰符必须是public,url才能被拦截. internal能编译通过,但无法拦截url请求.priv ...

  6. [ASP.NET MVC 小牛之路]09 - Controller 和 Action (1)

    我们知道,在 MVC 中每个请求都会提交到 Controller 进行处理.Controller 是和请求密切相关的,它包含了对请求的逻辑处理,能对 Model 进行操作并选择 View 呈现给用户, ...

  7. ASP.NET MVC 4 (四) 控制器扩展

    MVC的标准流程是请求传递给控制器,由控制器action方法操作数据模型,最后交由视图渲染输出,这里忽略了两个细节,就是MVC是如何创建相应控制器实例,又是如何调用控制器action方法的,这就必须讲 ...

  8. ASP.NET MVC* 采用Unity依赖注入Controller

    Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...

  9. ASP.NET MVC验证标注的扩展-checkbox必选

    我们知道ASP.NET mvc提供一些表单的验证标注,比如必填属性RequiredAttribute 但是这个属性不适合选择框的必选 但是很多时候,我们却是需要一些必选的单选框 比如网站注册的时候,需 ...

随机推荐

  1. c# 元组Tuple

    Tuple类型像一个口袋,在出门前可以把所需的任何东西一股脑地放在里面.您可以将钥匙.驾驶证.便笺簿和钢笔放在口袋里,您的口袋是存放各种东西的收集箱.C# 4.0引入的一个新特性 Tuple类型与口袋 ...

  2. 梦想还需有,因它必实现——发现最新版iOS漏洞,OverSky团队专访

    梦想还需有,因它必实现——发现最新版iOS漏洞,OverSky团队专访    “成功了!”,随着一句欢呼声在阿里巴巴西溪园区传出,Cydia的图标出现在一部iOS9.3.4的iPhone6上并成功运行 ...

  3. Express4 启航指南

    确实有感而发,Nodejs真的发展太快了,这么说的原因有两点:自己去年冬天买了本<了不起的Node.js>,里面介绍Express的版本还是2.x.x:前些天小伙伴买了本<Node. ...

  4. Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP

    <Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP. ...

  5. SSH实战 · SSH项目开发环境搭建

    一:SSH整合 创建一个新的WEB项目 引入struts2.3.15.3: jar包:                 struts-2.3.15.3\apps\struts2-blank.war\W ...

  6. [Linux]Linux下安装和配置solr/tomcat/IK分词器 详细实例一.

    在这里一下讲解着三个的安装和配置, 是因为solr需要使用tomcat和IK分词器, 这里会通过图文教程的形式来详解它们的安装和使用.注: 本文属于原创文章, 如若转载,请注明出处, 谢谢.关于设置I ...

  7. PHP面向对象

    面向对象 1.类由众多的对象抽象出来的    2.对象    一起皆对象    由类实例化出来的 求两个圆之间阴影的面积 $sr1=10; $sr2=5; $mj=3.14*$sr1*$sr1-3.1 ...

  8. IM消息送达保证机制实现(二):保证离线消息的可靠投递

    1.前言 本文的上篇<IM消息送达保证机制实现(一):保证在线实时消息的可靠投递>中,我们讨论了在线实时消息的投递可以通过应用层的确认.发送方的超时重传.接收方的去重等手段来保证业务层面消 ...

  9. 单页面实现之hash

    至学了angularJs后,发现这个单页面应用不知道在没有angularJs的情况下怎么实现. 所以就此对这个思考与资料并行,终于知道这个的实现基本原理. 首先angularJs的实现是hash值的变 ...

  10. js实现继承的方式总结

    js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...