C# Model对象实例转换为URL网址参数Param形式
作者:C/S框架网|www.cscode.ne  发布日期:2021-01-13 11:15:24
  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



上一篇 下一篇