C# 构造基础返回值类型-BaseResponse
学无止境,精益求精
十年河东,十年河西,莫欺少年穷
用于基础返回值类型,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Data.DataCommon
{
public class BaseResponse
{
public BaseResponse()
{
this.IsSuccess = false;
this.ResultCode = -;
this.ResultMessage = "请求失败...";
}
/// <summary>
/// 返回信息
/// </summary>
public string ResultMessage { get; set; }
/// <summary>
/// 返回编码 -1 代表失败 0代表成功
/// </summary>
public int ResultCode { get; set; }
/// <summary>
/// 处理是否成功
/// </summary>
public bool IsSuccess { get; set; }
} public class BaseResponse<T> : BaseResponse
{
public T Data { get; set; } //public List<T> DataList { get; set; } public BaseResponse()
{
this.IsSuccess = false;
this.ResultCode = -;
this.ResultMessage = "请求失败...";
} public BaseResponse(T data)
{
this.Data = data;
}
} public class CommonBaseResponse
{
#region 重置Response
public static BaseResponse<T> SetResponse<T>(T Data, bool bol, string Msg = "", int cord = )
{
BaseResponse<T> response = new BaseResponse<T>();
response.Data = Data;
response.IsSuccess = bol;
response.ResultCode = bol == true ? : -;
if (cord != )
{
response.ResultCode = cord;
}
response.ResultMessage = bol == true ? "请求成功..." : "请求失败...";
if (!string.IsNullOrEmpty(Msg))
{
response.ResultMessage = Msg;
}
return response;
}
public static BaseResponse SetResponse(bool bol, string Msg = "", int cord = )
{
BaseResponse response = new BaseResponse();
response.IsSuccess = bol;
response.ResultCode = bol == true ? : -;
if (cord != )
{
response.ResultCode = cord;
}
response.ResultMessage = bol == true ? "请求成功..." : "请求失败...";
if (!string.IsNullOrEmpty(Msg))
{
response.ResultMessage = Msg;
}
return response;
}
#endregion
}
}
下面我们用GET,POST请求如下:
[HttpPost]
public ActionResult GetData(DataModel model)
{
var result = CommonBaseResponse.SetResponse(true);
return Json(result, JsonRequestBehavior.DenyGet);
} [HttpGet]
public ActionResult GetData(string userId)
{
var result=CommonBaseResponse.SetResponse(true);
return Json(result, JsonRequestBehavior.AllowGet);
}
Ajax 请求如下:
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function () {
$.get("/Home/GetData?userId=chen", function (result) {
console.log(result)
})
})
</script>
返回值如下:
Post请求类似,在此不再累述。
如果返回值中带有数据集合,怎么处理?
C#如下:
[HttpGet]
public ActionResult GetData(string userId)
{
List<DataModel> list = new List<DataModel>();
list.Add(new DataModel() { userId = "", userName = "Jack.Chen", userNo = "", userSex = "男" });
list.Add(new DataModel() { userId = "", userName = "LiLi.Chen", userNo = "", userSex = "女" });
var result = CommonBaseResponse.SetResponse<List<DataModel>>(list,true);
return Json(result, JsonRequestBehavior.AllowGet);
}
返回值如下:
综上所述,如此简单。
顺便一个格式化类
public class CommonModelFunc
{ public static string FormatDate(Nullable<System.DateTime> date)
{
try
{
string dateString = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
if (dateString != "0001-01-01")
{
return Convert.ToDateTime(date).ToString("yyyy-MM-dd");
}
else
{
return "";
}
}
catch
{
return "";
}
} public static string FormatDate(Nullable<System.DateTime> date,string format)
{
try
{
string dateString = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
if (dateString != "0001-01-01")
{
return Convert.ToDateTime(date).ToString(format);
}
else
{
return "";
}
}
catch
{
return "";
}
} public static string FormatStringDate(string date)
{
try
{
string dateString = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
if (dateString != "0001-01-01")
{
return Convert.ToDateTime(date).ToString("yyyy-MM-dd");
}
else
{
return "";
}
}
catch
{
return "";
}
} public static string FormatStringDate(string date,string format)
{
try
{
string dateString = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
if (dateString != "0001-01-01")
{
return Convert.ToDateTime(date).ToString(format);
}
else
{
return "";
}
}
catch
{
return "";
}
} public static string FormatMoney(Nullable<decimal> Money)
{
if (Money.HasValue)
{
return Convert.ToDouble(Money).ToString("0.00");
}
else
{
return "";
}
} public static string FormatDateHMS(Nullable<System.DateTime> date)
{
try
{
string dateString = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
if (dateString != "0001-01-01")
{
return Convert.ToDateTime(date).ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
return "";
} }
catch
{
return "";
}
} public static string FormatDateChineseHMS(Nullable<System.DateTime> date)
{
try
{
string dateString = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
if (dateString != "0001-01-01")
{
return Convert.ToDateTime(date).ToString("yyyy年MM月dd日 HH时mm分");
}
else
{
return "";
} }
catch
{
return "";
}
}
}
在此,顺便介绍一个ConvertHelper的类,如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;
namespace Movit.ZhaoCai.DataCommon
{
public static class ConvertHelper
{ /// <summary>
/// datatow 转换Datatable
/// </summary>
/// <param name="drArr"></param>
/// <returns></returns>
public static DataTable GetDataTableByDataRows(DataRow[] drArr)
{
if (drArr == null || drArr.Length == ) return null;
DataTable tmp = drArr[].Table.Clone(); // 复制DataRow的表结构
foreach (DataRow row in drArr)
tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
return tmp; } public static string ToString(object value)
{
if (value != null)
{
return value.ToString();
}
else
{
return string.Empty;
}
} public static int ToInt(object value)
{
int result = -;
if (value != null)
{
int.TryParse(value.ToString(), out result);
}
return result;
} public static decimal ToDecimal(object value)
{
decimal val = ;
if (value == null || string.IsNullOrEmpty(value.ToString()))
{ }
else
{
if (value != null && IsNumber(value.ToString()))
{ decimal.TryParse(value.ToString(), out val); } }
return Math.Round(val, );
}
public static decimal? ToEngineeringquantityDecimal(object value)
{
decimal val = ;
if (value == null || string.IsNullOrEmpty(value.ToString()))
{
return null;
}
else
{
if (value != null && IsNumber(value.ToString()))
{ decimal.TryParse(value.ToString(), out val); } }
return Math.Round(val, );
}
public static DateTime ToDateTime(object value)
{
DateTime val = DateTime.Now;
if (value != null)
{
DateTime.TryParse(value.ToString(), out val);
}
return val;
}
public static double ToDouble(object value)
{
double val = ;
if (value != null)
{
double.TryParse(value.ToString(), out val);
}
return val;
} /// <summary>
/// Add by Allen.Yang,2017年11月20日 17:24:11
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static decimal ConvertToDecimal(this string str)
{
decimal d = ;
decimal.TryParse(str, out d);
return d;
} /// <summary>
/// 金额转换成大写金额
/// </summary>
/// <param name="money"></param>
/// <returns></returns>
public static string ConvertNumToUpper(string LowerMoney)
{
string functionReturnValue = null;
bool IsNegative = false; // 是否是负数
if (LowerMoney.Trim().Substring(, ) == "-")
{
// 是负数则先转为正数
LowerMoney = LowerMoney.Trim().Remove(, );
IsNegative = true;
}
string strLower = null;
string strUpart = null;
string strUpper = null;
int iTemp = ;
// 保留两位小数 123.489→123.49 123.4→123.4
LowerMoney = Math.Round(double.Parse(LowerMoney), ).ToString();
if (LowerMoney.IndexOf(".") > )
{
if (LowerMoney.IndexOf(".") == LowerMoney.Length - )
{
LowerMoney = LowerMoney + "";
}
}
else
{
LowerMoney = LowerMoney + ".00";
}
strLower = LowerMoney;
iTemp = ;
strUpper = "";
while (iTemp <= strLower.Length)
{
switch (strLower.Substring(strLower.Length - iTemp, ))
{
case ".":
strUpart = "圆";
break;
case "":
strUpart = "零";
break;
case "":
strUpart = "壹";
break;
case "":
strUpart = "贰";
break;
case "":
strUpart = "叁";
break;
case "":
strUpart = "肆";
break;
case "":
strUpart = "伍";
break;
case "":
strUpart = "陆";
break;
case "":
strUpart = "柒";
break;
case "":
strUpart = "捌";
break;
case "":
strUpart = "玖";
break;
} switch (iTemp)
{
case :
strUpart = strUpart + "分";
break;
case :
strUpart = strUpart + "角";
break;
case :
strUpart = strUpart + "";
break;
case :
strUpart = strUpart + "";
break;
case :
strUpart = strUpart + "拾";
break;
case :
strUpart = strUpart + "佰";
break;
case :
strUpart = strUpart + "仟";
break;
case :
strUpart = strUpart + "万";
break;
case :
strUpart = strUpart + "拾";
break;
case :
strUpart = strUpart + "佰";
break;
case :
strUpart = strUpart + "仟";
break;
case :
strUpart = strUpart + "亿";
break;
case :
strUpart = strUpart + "拾";
break;
case :
strUpart = strUpart + "佰";
break;
case :
strUpart = strUpart + "仟";
break;
case :
strUpart = strUpart + "万";
break;
default:
strUpart = strUpart + "";
break;
} strUpper = strUpart + strUpper;
iTemp = iTemp + ;
} strUpper = strUpper.Replace("零拾", "零");
strUpper = strUpper.Replace("零佰", "零");
strUpper = strUpper.Replace("零仟", "零");
strUpper = strUpper.Replace("零零零", "零");
strUpper = strUpper.Replace("零零", "零");
strUpper = strUpper.Replace("零角零分", "整");
strUpper = strUpper.Replace("零分", "整");
strUpper = strUpper.Replace("零角", "零");
strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
strUpper = strUpper.Replace("亿零万零圆", "亿圆");
strUpper = strUpper.Replace("零亿零万", "亿");
strUpper = strUpper.Replace("零万零圆", "万圆");
strUpper = strUpper.Replace("零亿", "亿");
strUpper = strUpper.Replace("零万", "万");
strUpper = strUpper.Replace("零圆", "圆");
strUpper = strUpper.Replace("零零", "零"); // 对壹圆以下的金额的处理
if (strUpper.Substring(, ) == "圆")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "零")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "角")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "分")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "整")
{
strUpper = "零圆整";
}
functionReturnValue = strUpper; if (IsNegative == true)
{
return "负" + functionReturnValue;
}
else
{
return functionReturnValue;
}
} /// <summary>
/// BPM加密字符串key生产规则
/// </summary>
/// <param name="str">BPM流程号</param>
/// <returns></returns>
public static string NewGetEnCodeStr(string str)
{
string enStr = "";
if (str != null && str.Length > )
{
str = (((long.Parse(str) * - ) + ) * ).ToString();
int l = str.ToString().Length;
if (l % == )//当字符长度为偶数时
{
enStr = str.Remove(l / , );
}
else
{
enStr = str.Remove(l / + , );//从字符中间+1的位置移除2个字符
}
}
return enStr;
} /// <summary>
/// 加密字符串2生产规则
/// </summary>
/// <param name="content">当前查看用户账号</param>
/// <returns></returns>
public static string Encrypt(string content)
{
byte[] enData = Encoding.UTF8.GetBytes(content);
return Convert.ToBase64String(enData);
} /// <summary>
/// 转换成Datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
} foreach (T item in items)
{
var values = new object[props.Length]; for (int i = ; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
} tb.Rows.Add(values);
} return tb;
} /// <summary>
/// 确定指定的类型为空。
/// </summary>
private static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
} /// <summary>
/// 如果类型为Nullable,返回底层类型,否则返回类型。
/// </summary>
private static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
} /// <summary>
/// 金额转换成中文大写金额
/// </summary>
/// <param name="LowerMoney">eg:10.74</param>
/// <returns></returns>
public static string MoneyToUpper(string LowerMoney)
{
string functionReturnValue = null;
bool IsNegative = false; // 是否是负数
if (LowerMoney.Trim().Substring(, ) == "-")
{
// 是负数则先转为正数
LowerMoney = LowerMoney.Trim().Remove(, );
IsNegative = true;
}
string strLower = null;
string strUpart = null;
string strUpper = null;
int iTemp = ;
// 保留两位小数 123.489→123.49 123.4→123.4
LowerMoney = Math.Round(double.Parse(LowerMoney), ).ToString();
if (LowerMoney.IndexOf(".") > )
{
if (LowerMoney.IndexOf(".") == LowerMoney.Length - )
{
LowerMoney = LowerMoney + "";
}
}
else
{
LowerMoney = LowerMoney + ".00";
}
strLower = LowerMoney;
iTemp = ;
strUpper = "";
while (iTemp <= strLower.Length)
{
switch (strLower.Substring(strLower.Length - iTemp, ))
{
case ".":
strUpart = "圆";
break;
case "":
strUpart = "零";
break;
case "":
strUpart = "壹";
break;
case "":
strUpart = "贰";
break;
case "":
strUpart = "叁";
break;
case "":
strUpart = "肆";
break;
case "":
strUpart = "伍";
break;
case "":
strUpart = "陆";
break;
case "":
strUpart = "柒";
break;
case "":
strUpart = "捌";
break;
case "":
strUpart = "玖";
break;
} switch (iTemp)
{
case :
strUpart = strUpart + "分";
break;
case :
strUpart = strUpart + "角";
break;
case :
strUpart = strUpart + "";
break;
case :
strUpart = strUpart + "";
break;
case :
strUpart = strUpart + "拾";
break;
case :
strUpart = strUpart + "佰";
break;
case :
strUpart = strUpart + "仟";
break;
case :
strUpart = strUpart + "万";
break;
case :
strUpart = strUpart + "拾";
break;
case :
strUpart = strUpart + "佰";
break;
case :
strUpart = strUpart + "仟";
break;
case :
strUpart = strUpart + "亿";
break;
case :
strUpart = strUpart + "拾";
break;
case :
strUpart = strUpart + "佰";
break;
case :
strUpart = strUpart + "仟";
break;
case :
strUpart = strUpart + "万";
break;
default:
strUpart = strUpart + "";
break;
} strUpper = strUpart + strUpper;
iTemp = iTemp + ;
} strUpper = strUpper.Replace("零拾", "零");
strUpper = strUpper.Replace("零佰", "零");
strUpper = strUpper.Replace("零仟", "零");
strUpper = strUpper.Replace("零零零", "零");
strUpper = strUpper.Replace("零零", "零");
strUpper = strUpper.Replace("零角零分", "整");
strUpper = strUpper.Replace("零分", "整");
strUpper = strUpper.Replace("零角", "零");
strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
strUpper = strUpper.Replace("亿零万零圆", "亿圆");
strUpper = strUpper.Replace("零亿零万", "亿");
strUpper = strUpper.Replace("零万零圆", "万圆");
strUpper = strUpper.Replace("零亿", "亿");
strUpper = strUpper.Replace("零万", "万");
strUpper = strUpper.Replace("零圆", "圆");
strUpper = strUpper.Replace("零零", "零"); // 对壹圆以下的金额的处理
if (strUpper.Substring(, ) == "圆")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "零")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "角")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "分")
{
strUpper = strUpper.Substring(, strUpper.Length - );
}
if (strUpper.Substring(, ) == "整")
{
strUpper = "零圆整";
}
functionReturnValue = strUpper; if (IsNegative == true)
{
return "负" + functionReturnValue;
}
else
{
return functionReturnValue;
}
} #region 泛型集合和DataSet互转 请注意,使用此方法时请确保实体集合里没有索引属性
public static IList<T> DataTableToIList<T>(DataTable p_Data)
{
// 返回值初始化
IList<T> result = new List<T>(); if (p_Data == null)
return result; for (int j = ; j < p_Data.Rows.Count; j++)
{
T _t = (T)Activator.CreateInstance(typeof(T));
System.Reflection.PropertyInfo[] propertys = _t.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo pi in propertys)
{
for (int i = ; i < p_Data.Columns.Count; i++)
{
// 属性与字段名称一致的进行赋值
string columnName = p_Data.Columns[i].ColumnName.ToUpper();
//try
//{ if (pi.Name.ToUpper().Equals(columnName) || pi.Name.ToUpper().Equals(columnName.Replace("_", string.Empty)))
{
// 数据库NULL值单独处理
if (p_Data.Rows[j][i] != DBNull.Value && p_Data.Rows[j][i] != null)
{
if (pi.PropertyType == typeof(Boolean))
{
pi.SetValue(_t, Convert.ToBoolean(p_Data.Rows[j][i]), null);
}
else if (pi.PropertyType == typeof(Int16))
{
pi.SetValue(_t, Convert.ToInt16(p_Data.Rows[j][i]), null);
}
else if (pi.PropertyType == typeof(Int32))
{
pi.SetValue(_t, Convert.ToInt32(p_Data.Rows[j][i]), null);
}
else if (pi.PropertyType == typeof(Decimal))
{
pi.SetValue(_t, Convert.ToDecimal(p_Data.Rows[j][i]), null);
}
else if (pi.PropertyType == typeof(Int32?))
{
pi.SetValue(_t, Convert.ToInt32(p_Data.Rows[j][i]), null);
}
else if (pi.PropertyType == typeof(DateTime))
{
pi.SetValue(_t, Convert.ToDateTime(p_Data.Rows[j][i]), null);
}
else
{
pi.SetValue(_t, p_Data.Rows[j][i], null);
}
}
else
pi.SetValue(_t, null, null);
break;
}
//}
//catch(Exception ex)
//{
// string s= columnName;
//}
}
}
result.Add(_t);
} return result;
}
/// <summary>
/// 集合装换DataSet
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataSet ToDataSet(IList p_List)
{
DataSet result = new DataSet();
DataTable _DataTable = new DataTable();
if (p_List.Count > )
{
PropertyInfo[] propertys = p_List[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
_DataTable.Columns.Add(pi.Name, pi.PropertyType);
} for (int i = ; i < p_List.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(p_List[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
_DataTable.LoadDataRow(array, true);
}
}
result.Tables.Add(_DataTable);
return result;
} /// <summary>
/// 泛型集合转换DataSet
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p_List">泛型集合</param>
/// <param name="p_PropertyName">待转换属性名数组</param>
/// <returns></returns>
public static DataSet ToDataSet<T>(List<T> p_List)
{
DataSet result = new DataSet();
DataTable _DataTable = ToDataTable<T>(p_List); result.Tables.Add(_DataTable);
return result;
} /// <summary>
/// DataSet装换为泛型集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p_DataSet">DataSet</param>
/// <param name="p_TableIndex">待转换数据表索引</param>
/// <returns></returns>
public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
{
if (p_DataSet == null || p_DataSet.Tables.Count < )
return null;
if (p_TableIndex > p_DataSet.Tables.Count - )
return null;
if (p_TableIndex < )
p_TableIndex = ; DataTable p_Data = p_DataSet.Tables[p_TableIndex];
// 返回值初始化
IList<T> result = new List<T>();
for (int j = ; j < p_Data.Rows.Count; j++)
{
T _t = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] propertys = _t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
for (int i = ; i < p_Data.Columns.Count; i++)
{
// 属性与字段名称一致的进行赋值
if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
{
// 数据库NULL值单独处理
if (p_Data.Rows[j][i] != DBNull.Value)
pi.SetValue(_t, p_Data.Rows[j][i], null);
else
pi.SetValue(_t, null, null);
break;
}
}
}
result.Add(_t);
}
return result;
} /// <summary>
/// DataSet装换为泛型集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p_DataSet">DataSet</param>
/// <param name="p_TableName">待转换数据表名称</param>
/// <returns></returns>
public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
{
int _TableIndex = ;
if (p_DataSet == null || p_DataSet.Tables.Count < )
return null;
if (string.IsNullOrEmpty(p_TableName))
return null;
for (int i = ; i < p_DataSet.Tables.Count; i++)
{
// 获取Table名称在Tables集合中的索引值
if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
{
_TableIndex = i;
break;
}
}
return DataSetToIList<T>(p_DataSet, _TableIndex);
} /// <summary>
/// 将数值税率转换成显示模式(0.05=>5%)
/// </summary>
/// <returns></returns>
public static string RateForShow(string rate)
{
string result = string.Empty;
if (string.IsNullOrEmpty(rate))
{
return result;
}
rate = rate.Replace("%", "");
decimal? i = ToDecimal(rate) * ;
if (i.HasValue)
{
if (i.Value <= )
{
return "其它";
} else
{
return i.Value.ToString("#0") + "%";
}
}
else
{
return "";
}
} /// <summary>
/// 将金额转换为千分位个是(100,000.12)
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string DecimalToStringN2(object value)
{
decimal val = ;
if (value != null)
{
val = StringN2ToDecimal(value.ToString());
} return string.Format("{0:N2}", val);
} /// <summary>
/// 将千分位显示的金额字符转换为金额(100,000.12)
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static decimal StringN2ToDecimal(string value)
{
decimal val = ;
if (!string.IsNullOrEmpty(value))
{
value = value.Replace(",", "");
Decimal.TryParse(value, out val);
}
return val;
} #endregion /// <summary>
/// 验证是否是数字
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsNumber(string input)
{
string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
} /// <summary>
/// 将阿拉伯数字转换成中文数字
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ConvertArabicNumToChineseNum(object value)
{
if (value != null)
{
string res = string.Empty;
switch (value.ToString())
{
case "":
res = "一";
break;
case "":
res = "二";
break;
case "":
res = "三";
break;
case "":
res = "四";
break;
case "":
res = "五";
break;
case "":
res = "六";
break;
case "":
res = "七";
break;
case "":
res = "八";
break;
case "":
res = "九";
break;
case "":
res = "十";
break;
}
return res;
}
else { return string.Empty; }
}
}
}
@陈卧龙的博客
C# 构造基础返回值类型-BaseResponse的更多相关文章
- C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解 ...
- WebApi 接口返回值类型详解 ( 转 )
使用过Webapi的园友应该都知道,Webapi的接口返回值主要有四种类型 void无返回值 IHttpActionResult HttpResponseMessage 自定义类型 此篇就围绕这四块分 ...
- WebApi接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.作为程序猿,我们都知道参数和返回值是编程领域不可分割的两大块,此前分享了 ...
- 第18课 类型萃取(2)_获取返回值类型的traits
1. 获取可调用对象返回类型 (1)decltype:获取变量或表达式的类型(见第2课) (2)declval及原型 ①原型:template<class T> T&& d ...
- WebApi 接口返回值不困惑:返回值类型详解。IHttpActionResult、void、HttpResponseMessage、自定义类型
首先声明,我还没有这么强大的功底,只是感觉博主写的很好,就做了一个复制,请别因为这个鄙视我,博主网址:http://www.cnblogs.com/landeanfen/p/5501487.html ...
- (转)C# WebApi 接口返回值不困惑:返回值类型详解
原文地址:http://www.cnblogs.com/landeanfen/p/5501487.html 正文 前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi ...
- [转]C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
本文转自:http://www.cnblogs.com/landeanfen/p/5501487.html 阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T c ...
- C#进阶系列——WebApi接口返回值类型详解
阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T content) 2.Ok(). Ok(T content) 3.NotFound() 4.其他 5.自定义I ...
- Web Api 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 WebApi 接口参数:传参详解,这篇博文内容本身很基础 ...
随机推荐
- 语义分析的waf 目前就看到长亭 机器学习的waf有fortnet 阿里云的waf也算
近期,在全球权威咨询机构 Gartner 发布的 2019 Web 应用防火墙魔力象限中,阿里云 Web 应用防火墙成功入围,是亚太地区唯一一家进入该魔力象限的厂商! Web 应用防火墙,简称 WAF ...
- 【使用篇二】SpringBoot整合jsp(6)
1. pom.xml中添加jstl和jasper springboot不推荐使用jsp,所以在spring-boot-starter-web启动器中并没有包括这两个,所以我们需要单独引入: <! ...
- djang小项目过程中的小问题 01(django中的configrarion配置、django自带命名规范)
不知道为什么,明明照着做,但是我就会遇到一些错误问题,但是我觉着自己生下来就是解决问题的. ##1. 第一个是在django运行时,没有配置configration, 这个时候我添加一个django ...
- 残差residual VS 误差 error
https://blog.csdn.net/jmydream/article/details/8764869 In statistics and optimization, statistical e ...
- Comet OJ 计算机(computer)
Comet OJ 计算机(computer) 题目传送门 题目描述 小 X 有一台奇怪的计算机. 这台计算机首先会读入一个正整数 nn,然后生成一个包含 nn 个数的序列 aa. 一开始 a_i(1 ...
- source ~/.bashrc
编辑命令: gedit ~/.bashrc source ~/.bashrc 每次修改.bashrc后,使用source ~/.bashrc(或者 . ~/.bashrc)就可以立刻加载修改后的设置, ...
- EventBus事件总线(牛x版)
事件总线: public interface IEventBus { void Trigger<TEvent>(TEvent eventData, string topic = null) ...
- day 24
I am a slow walker, but I never walk back. 我走得很慢,但是我从来不会后退.
- 分布式事物解决方案-TCC
分布式框架下,如何保证事物一致性一直是一个热门话题.当然事物一致性解决方案有很多种(请参考:分布式事物一致性设计思路),我们今天主要介绍TCC方案解决的思路.以下是参与设计讨论的一种解决思路,大家有问 ...
- (十六)golang--匿名函数
Go支持匿名函数,如果我们某个函数只是使用一次,可以考虑使用匿名函数,匿名函数也可以实现多次调用: 匿名函数的使用方式:(1)在定义匿名函数的时候就直接调用,这种方式匿名函数只调用一次: (2)将匿名 ...