我想从我的网址中逐个删除“ids”查询字符串。我怎样才能做到这一点 ? (使用Asp.net4.0,c#)
Default.aspx?ids=10,2,6,5
我想删除“ids = 6”,但语言将是第一个,中间或最后一个,所以我会这样:
Default.aspx?ids=10,2,5,
答案 0 :(得分:1)
步骤1.通过以下方式将您的ID放在数组中: -
string[] idsarray = Request.QueryString["ids"].ToString().Split(',');
步骤2.创建一个按照您的语言删除的功能
string removeidat(string[] id, string at)
{
string toren = "";
int remat = -1;
if (at=="first")
{
remat = 0;
}
else if (at == "middle")
{
remat = id.Length / 2;
}
else
{
remat = id.GetUpperBound(0);
}
for (int i = 0; i < id.GetUpperBound(0); i++)
{
if (i!=remat)
{
toren += id[i] + ",";
}
}
if (toren.Length>0)
{
toren = toren.Substring(0, toren.Length - 1);
}
return toren;
}
示例:如果要删除最后一个ID,则代码将为
string[] idsarray = Request.QueryString["ids"].ToString().Split(',');
string newids = removeidat(idsarray , "last")
答案 1 :(得分:0)
string strIDs = Request.QueryString["ids"];
if(strIDs != null)
{
string[] ids = strIDs.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
var no6 = ids.Where(id => id != "6");
string newUrl = string.Format("Default.aspx?ids={0}", string.Join(",", no6));
Response.Redirect(newUrl);
}