星空网 > 软件开发 > ASP.net

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

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

当然,你也可以用ActionResult或者ContentResult,然后直接返回

如果我们想要想JsonResult一样来调用和返回

 

第一步,扩展System.Web.Mvc

/// <summary>/// 扩展System.Web.Mvc /// 指定是否允许来自客户端的HTTP GET请求/// 熊仔其人/// </summary>public enum /// <summary>  /// HTTP GET requests from the client are allowed.  /// 允许来自客户端的HTTP GET请求  /// </summary>     AllowGet = 0,  /// <summary>  /// HTTP GET requests from the client are not allowed.  /// 不允许来自客户端的HTTP GET请求  /// </summary>  DenyGet = 1,}

 

第二步,实现

/// <summary>/// 实现/// 扩展MVC的ActionResult支持返回/// 熊仔其人/// </summary>public class /// <summary>  /// Initializes a new instance of the System.Web.Mvc./// 初始化  /// </summary>       public /// <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 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 (string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))    {      throw new InvalidOperationException("");    }    HttpResponseBase response = context.HttpContext.Response;    response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/";    if (this.ContentEncoding != null)    {      response.ContentEncoding = this.ContentEncoding;    }    if (Data != null)    {      using (MemoryStream ms = new MemoryStream())      {        = new // 把数据序列化到内存流中                   ms.Position = 0;        using (StreamReader sr = new StreamReader(ms))        {          context.HttpContext.Response.Output.Write(sr.ReadToEnd()); // 输出流对象         }      }    }  }}

 

第三步,扩展System.Mvc.Controller

/// <summary>/// 扩展System.Mvc.Controller/// 熊仔其人/// </summary>public static class ControllerExtension{  public static this Controller request, object obj) { return null, null, public static this Controller request, object obj, return null, null, behavior); }  public static this Controller request, object obj, Encoding contentEncoding, return null, contentEncoding, behavior); }  public static this Controller request, object obj, string contentType, Encoding contentEncoding, return internal static object data, string contentType, Encoding contentEncoding, return new  behavior }; }}

 

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

第四步,在Controller里调用

public ActionResult GetActionResult(string type){  var data = new List<string>(); //注意,data必须是可被序列化的内容  data.Add("A");  data.Add("B");  data.Add("C");  if (type.ToLower() == "")  {    return this.else if (type.ToLower() == "json")  {    return Json(data, JsonRequestBehavior.AllowGet);  }  else {         //error messages           return View("不支持此方法");  }}public var data = new List<string>(); //注意,data必须是可被序列化的内容  data.Add("A");  data.Add("B");  data.Add("C");  return this.

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

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

<?<ArrayOfString <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.namespace Onexz.API.Models{  /// <summary>  /// 扩展System.Web.Mvc /// 指定是否允许来自客户端的HTTP GET请求  /// 熊仔其人/// </summary>  public enum /// <summary>    /// HTTP GET requests from the client are allowed.    /// 允许来自客户端的HTTP GET请求    /// </summary>       AllowGet = 0,    /// <summary>    /// HTTP GET requests from the client are not allowed.    /// 不允许来自客户端的HTTP GET请求    /// </summary>    DenyGet = 1,  }  /// <summary>  /// 实现/// 扩展MVC的ActionResult支持返回/// 熊仔其人/// </summary>  public class /// <summary>    /// Initializes a new instance of the System.Web.Mvc./// 初始化    /// </summary>         public /// <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 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 (string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))      {        throw new InvalidOperationException("");      }      HttpResponseBase response = context.HttpContext.Response;      response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/";      if (this.ContentEncoding != null)      {        response.ContentEncoding = this.ContentEncoding;      }      if (Data != null)      {        using (MemoryStream ms = new MemoryStream())        {          = new // 把数据序列化到内存流中                     ms.Position = 0;          using (StreamReader sr = new StreamReader(ms))          {            context.HttpContext.Response.Output.Write(sr.ReadToEnd()); // 输出流对象           }        }      }    }  }  /// <summary>  /// 扩展System.Mvc.Controller  /// 熊仔其人/// </summary>  public static class ControllerExtension  {    public static this Controller request, object obj) { return null, null, public static this Controller request, object obj, return null, null, behavior); }    public static this Controller request, object obj, Encoding contentEncoding, return null, contentEncoding, behavior); }    public static this Controller request, object obj, string contentType, Encoding contentEncoding, return internal static object data, string contentType, Encoding contentEncoding, return new  behavior }; }  }}

 




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

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

黑色:https://www.goluckyvip.com/tag/10920.html
办公家具:https://www.goluckyvip.com/tag/10921.html
跨境出口电商:https://www.goluckyvip.com/tag/10922.html
央行降息:https://www.goluckyvip.com/tag/10923.html
开发信模板:https://www.goluckyvip.com/tag/10924.html
自动广告:https://www.goluckyvip.com/tag/10925.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流