using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using XiaoFeng;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Data;
namespace XiaoFeng
{
/// <summary>
/// 输出消息
/// Version : 1.0
/// Create Date : 2016-12-23
/// Author : jacky
/// Site : www.zhuovi.com
/// </summary>
[Serializable]
[XmlRoot("Root")]
public class ResponseMessage<T>
{
#region 构造器
/// <summary>
/// 无参构造器
/// </summary>
public ResponseMessage() : this(ResponseState.success, "", default(T)) { }
/// <summary>
/// 设置信息
/// </summary>
/// <param name="state">状态</param>
/// <param name="message">信息</param>
/// <param name="data">数据</param>
public ResponseMessage(ResponseState state, string message = "", T data = default(T))
{
this.state = state; this.data = data; this.message = message;
}
#endregion
#region 属性
/// <summary>
/// 状态
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public ResponseState state { get; set; }
/// <summary>
/// 消息
/// </summary>
public string message { get; set; }
/// <summary>
/// 数据 如果是DataTable 一定要有TableName值
/// </summary>
public T data { get; set; }
/// <summary>
/// 备用数据字段
/// </summary>
public dynamic other { get; set; }
#endregion
#region 方法
#region 输出数据
#region 输出JSON
/// <summary>
/// 输出JSON
/// </summary>
public void ToJson() { this.Write(EnumHelper.WriteType.JSON); }
#endregion
#region 输出XML
/// <summary>
/// 输出XML
/// </summary>
public void ToXml() { this.Write(EnumHelper.WriteType.XML); }
#endregion
#region 输出成功
/// <summary>
/// 输出成功
/// </summary>
/// <param name="writeType">输出类型</param>
public void Success(EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
{
this.state = ResponseState.success; this.Write(writeType);
}
/// <summary>
/// 输出成功
/// </summary>
/// <param name="data">数据</param>
/// <param name="writeType">输出类型</param>
public void Success(T data, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
{
this.data = data; this.Success(writeType);
}
#endregion
#region 输出错误
/// <summary>
/// 输出错误
/// </summary>
/// <param name="writeType">输出类型</param>
public void Error(EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
{
this.state = ResponseState.error; this.Write(writeType);
}
/// <summary>
/// 输出错误
/// </summary>
/// <param name="message">错误信息</param>
/// <param name="writeType">输出类型</param>
public void Error(string message, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
{
this.message = message; this.Error(writeType);
}
#endregion
#region 输出警告
/// <summary>
/// 输出警告
/// </summary>
/// <param name="WriteType">输出类型</param>
public void Warning(EnumHelper.WriteType WriteType = EnumHelper.WriteType.JSON)
{
this.state = ResponseState.warning; this.Write(WriteType);
}
/// <summary>
/// 输出警告
/// </summary>
/// <param name="message">警告信息</param>
/// <param name="writeType">输出类型</param>
public void Warning(string message, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
{
this.message = message; this.Warning(writeType);
}
#endregion
#region 输出
/// <summary>
/// 输出信息
/// </summary>
/// <param name="data">数据</param>
/// <param name="writeType">输出类型</param>
public void Write(T data, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
{
this.data = data; this.Write(writeType);
}
/// <summary>
/// 输出信息
/// </summary>
/// <param name="writeType">输出类型 默认 JSON ,XML,String</param>
public void Write(EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
{
HttpResponse Response= HttpContext.Current.Response;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Text.StringBuilder sbr = new System.Text.StringBuilder();
switch (writeType)
{
case EnumHelper.WriteType.JSON:
sbr.Append(JsonConvert.SerializeObject(this));
Response.ContentType = "text/json";
break;
case EnumHelper.WriteType.XML:
Response.ContentType = "text/xml";
if (this.data.GetType() == typeof(DataTable))
{
if ((this.data as DataTable).TableName.IsNullOrEmpty())
(this.data as DataTable).TableName = "item";
}
sbr.Append(XmlConvert.SerializerObject<ResponseMessage<T>>(this, "UTF-8", true));
sbr.Append("\n<!--生成于[{0}]-->".format(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
break;
case EnumHelper.WriteType.String:
Response.ContentType = "text/plain";
sbr.Append(String.Format("state:{0}\nmessage:{1}", this.state.ToString(), this.message.ToString()));
break;
}
Response.Write(sbr.ToString());
Response.Flush();
Response.End();
}
#endregion
#endregion
#region 返回序列化数据
/// <summary>
/// 返回序列化数据
/// </summary>
/// <param name="writeType">输出类型 默认 JSON ,XML,String</param>
/// <returns></returns>
public string getData(XiaoFeng.EnumHelper.WriteType writeType = XiaoFeng.EnumHelper.WriteType.JSON)
{
switch (writeType)
{
case EnumHelper.WriteType.JSON:
return JsonConvert.SerializeObject(this);
case EnumHelper.WriteType.XML:
if (this.data.GetType() == typeof(DataTable))
{
if ((this.data as DataTable).TableName.IsNullOrEmpty())
(this.data as DataTable).TableName = "item";
}
return XmlConvert.SerializerObject<ResponseMessage<T>>(this, "UTF-8", true);
case EnumHelper.WriteType.String:
return String.Format("state:{0}\nmessage:{1}", this.state.ToString(), this.message.ToString());
default:
return "";
}
}
#endregion
#endregion
}
#region 消息状态
/// <summary>
/// 消息状态
/// </summary>
public enum ResponseState
{
/// <summary>
/// 无
/// </summary>
none = 0,
/// <summary>
/// 成功
/// </summary>
success = 200,
/// <summary>
/// 出错
/// </summary>
error = 500,
/// <summary>
/// 警告
/// </summary>
warning = 100
}
#endregion
#region 输出消息
/// <summary>
/// 输出消息
/// Version : 1.0
/// Create Date : 2016-12-23
/// Author : jacky
/// Site : www.zhuovi.com
/// </summary>
[Serializable]
[XmlRoot("Root")]
public class ResponseMessage : ResponseMessage<String>
{
#region 构造器
/// <summary>
/// 无参构造器
/// </summary>
public ResponseMessage() : this(ResponseState.success, "", "") { }
/// <summary>
/// 构造器
/// </summary>
/// <param name="state">状态</param>
/// <param name="message">消息</param>
/// <param name="data">数据</param>
public ResponseMessage(ResponseState state, string message = "", String data = "") : base(state, message, data) { }
#endregion
}
#endregion
}
C#通用输出类
最新推荐文章于 2024-09-29 22:21:40 发布