C# Model对象实例转换为URL网址参数Param形式
C# Model对象实例转换为URL网址参数Param形式
C# Code:
/// <summary>
/// Model对象转换为uri网址参数形式
/// </summary>
/// <param name="obj">Model对象</param>
/// <param name="url">前部分网址</param>
/// <returns></returns>
public static string GetUriParam(object obj, string url = "")
{
PropertyInfo[] propertis = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
StringBuilder sb = new StringBuilder();
sb.Append(url);
//sb.Append("?");
foreach (var p in propertis)
{
var v = p.GetValue(obj, null);
if (v == null) v = "";
sb.Append(p.Name);
sb.Append("=");
sb.Append(Uri.EscapeDataString(v.ToString()));//将字符串转换为它的转义表示形式,HttpUtility.UrlEncode是小写
sb.Append("&");
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// Model对象转换为uri网址参数形式
/// </summary>
/// <param name="obj">Model对象</param>
/// <param name="url">前部分网址</param>
/// <returns></returns>
public static string GetUriParam(object obj, string url = "")
{
PropertyInfo[] propertis = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
StringBuilder sb = new StringBuilder();
sb.Append(url);
//sb.Append("?");
foreach (var p in propertis)
{
var v = p.GetValue(obj, null);
if (v == null) v = "";
sb.Append(p.Name);
sb.Append("=");
sb.Append(Uri.EscapeDataString(v.ToString()));//将字符串转换为它的转义表示形式,HttpUtility.UrlEncode是小写
sb.Append("&");
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网