/// <summary>
/// Url字段值替换,无该字段则添加
/// </summary>
/// <param name="strRepSource">需要修改的源URL</param>
/// <param name="strRepField">字段,注意区分大小写。如:ID或Key37</param>
/// <param name="strNewValue">新值</param>
/// <returns>替换后新Url</returns>
public static string UrlReplace(string strRepSource, string strRepField, string strNewValue)
{
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
bool bSwitch = false;
if (strRepSource.Contains("&" + strRepField + "="))
{
strRepField = "&" + strRepField + "=";
bSwitch = true;
}
else if (strRepSource.Contains("?" + strRepField + "="))
{
strRepField = "?" + strRepField + "=";
bSwitch = true;
}
if (bSwitch)
{
string[] strArray = strRepSource.Split(new string[] { strRepField }, 2, StringSplitOptions.RemoveEmptyEntries);
strBuilder.Append(strArray[0]);
strBuilder.Append(strRepField);
strBuilder.Append(strNewValue);
if (strArray.Length == 2 && strArray[1].Contains("&"))
{
string[] strAy = strArray[1].Split(new char[] { ‘&‘ }, 2, StringSplitOptions.RemoveEmptyEntries);
strBuilder.Append("&" + strAy[strAy.Length - 1]);
}
}
else
{
strBuilder.Append(strRepSource);
strBuilder.Append(‘&‘);
strBuilder.Append(strRepField);
strBuilder.Append(‘=‘);
strBuilder.Append(strNewValue);
}
return strBuilder.ToString();
}
/// <summary>
/// Url字段值替换,无该字段则添加
/// </summary>
/// <param name="strRepSource">需要修改的源URL</param>
/// <param name="strRepField">字段,注意区分大小写。如:ID或Key37</param>
/// <param name="iNewValue">新值</param>
/// <returns>替换后新Url</returns>
public static string UrlReplace(string strRepSource, string strRepField, int iNewValue)
{
return UrlReplace(strRepSource, strRepField, iNewValue.ToString());
}
/// <summary>
/// Url构造函数
/// </summary>
/// <param name="strUrl">原Url</param>
/// <param name="strIdField">实体ID字段</param>
/// <param name="iIdValue">ID值</param>
/// <param name="strJvalue">选择值。实现对应选择卡凹陷</param>
/// <returns>构造后的Url,包括Key37和Key58值的添加</returns>
public static string UrlStructure(string strUrl, string strIdField, string iIdValue, string strJvalue)
{
strUrl = UrlReplace(strUrl, strIdField, iIdValue);
strUrl = UrlReplace(strUrl, "Key37", iIdValue);
strUrl = UrlReplace(strUrl, "Key58", iIdValue);
if (!string.IsNullOrEmpty(strJvalue))
{
strUrl = UrlReplace(strUrl, "J", strJvalue);
}
return strUrl;
}
/// <summary>
/// 添加参数
/// </summary>
public static string AddParameters(string url, string k, string v)
{
if (!string.IsNullOrEmpty(k))
{
url = UrlReplace(url, k, v);
}
return url;
}
/// <summary>
/// Url构造函数
/// </summary>
/// <param name="strUrl">原Url</param>
/// <param name="strIdField">实体ID字段</param>
/// <param name="iIdValue">ID值</param>
/// <returns>构造后的Url,包括Key37和Key58值的添加</returns>
public static string UrlStructure(string strUrl, string strIdField, int iIdValue)
{
strUrl = UrlStructure(strUrl, strIdField, iIdValue.ToString(), string.Empty);
return strUrl;
}
/// <summary>
/// Url构造函数
/// </summary>
/// <param name="strUrl">原Url</param>
/// <param name="strIdField">实体ID字段</param>
/// <param name="iIdValue">ID值</param>
/// <returns>构造后的Url,包括Key37和Key58值的添加</returns>
public static string UrlStructure(string strUrl, string strIdField, string iIdValue)
{
strUrl = UrlStructure(strUrl, strIdField, iIdValue, string.Empty);
return strUrl;
}
原文地址:http://blog.csdn.net/y_f123/article/details/37808639