88 lines
2.4 KiB
C#
88 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace Hncore.Infrastructure.Common
|
|
{
|
|
public class UrlHelper
|
|
{
|
|
#region 设置url参数
|
|
|
|
/// <summary>
|
|
/// 设置url参数
|
|
/// </summary>
|
|
/// <param name="paramName"></param>
|
|
/// <param name="paramValue"></param>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public static string SetUrlParam(string url, string paramName, string paramValue)
|
|
{
|
|
paramName = paramName.ToLower();
|
|
|
|
string currentUrl = url;
|
|
|
|
if (!string.IsNullOrEmpty(paramValue))
|
|
{
|
|
paramValue = HttpUtility.UrlEncode(paramValue);
|
|
}
|
|
|
|
if (!currentUrl.Contains("?"))
|
|
{
|
|
return currentUrl += "?" + paramName + "=" + paramValue;
|
|
}
|
|
|
|
List<string> paramItems = currentUrl.Split('?')[1].Split('&').ToList();
|
|
|
|
string paramItem = paramItems.SingleOrDefault(t => t.ToLower().Split('=')[0] == paramName);
|
|
|
|
if (!string.IsNullOrEmpty(paramItem))
|
|
{
|
|
return currentUrl.Replace(paramItem, paramName + "=" + paramValue);
|
|
}
|
|
else
|
|
{
|
|
if (currentUrl.Contains("?"))
|
|
{
|
|
currentUrl += "&";
|
|
}
|
|
else
|
|
{
|
|
currentUrl += "?";
|
|
}
|
|
return currentUrl + paramName + "=" + paramValue;
|
|
}
|
|
}
|
|
|
|
public static string SetUrlParam(string url, object paramObj)
|
|
{
|
|
var type = paramObj.GetType();
|
|
var properties = type.GetProperties();
|
|
|
|
foreach (var property in properties)
|
|
{
|
|
string name = property.Name;
|
|
|
|
object valueObj = property.GetValue(paramObj, null);
|
|
|
|
if (valueObj == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string value = valueObj.ToString();
|
|
|
|
url = SetUrlParam(url, name, value);
|
|
}
|
|
|
|
return url;
|
|
}
|
|
|
|
|
|
public static string ToUrlParam(IDictionary<string, string> kvs)
|
|
{
|
|
return string.Join("&", kvs.Select(m => $"{m.Key}={m.Value}"));
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |