URL 拼接多查询条件( NameValueCollection )

 

 

 

public static string ToQueryString(NameValueCollection nvc)
{
    StringBuilder sb = new StringBuilder();
    foreach (var key in nvc.AllKeys)
    {
        string value = nvc[key];
        //EscapeDataString就是对特殊字符进行uri编码
        sb.Append(key).Append("=")
            .Append(Uri.EscapeDataString(value)).Append("&");
    }
    return sb.ToString().Trim('&');//去掉最后一个多余的&
}

public static string RemoveQueryString(NameValueCollection nvc, string name)
{
    NameValueCollection newNVC = new NameValueCollection(nvc);
    newNVC.Remove(name);
    return ToQueryString(newNVC);
}

public static string UpdateQueryString(NameValueCollection nvc,
    string name, string value)
{
    NameValueCollection newNVC = new NameValueCollection(nvc);
    if (newNVC.AllKeys.Contains(name))
    {
        newNVC[name] = value;
    }
    else
    {
        newNVC.Add(name, value);
    }
    return ToQueryString(newNVC);
}

 

    <ul style="display: none">
        <li><a href="~/House/Search?@(Html.Raw(MVCHelper.RemoveQueryString(Request.QueryString,"MonthRent")))">全部</a></li>
        <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","*-100"))">100元以下</a></li>
        <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","100-200"))">100-200元</a></li>
        <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","201-500"))">201-500元</a></li>
        <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","501-800"))">501-800元</a></li>
        <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","801-1000"))">801-1000元</a></li>
        <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","1001-*"))">1000元以上</a></li>
    </ul>

 

posted @ 2018-08-08 15:09  linyongqin  阅读(127)  评论(0编辑  收藏  举报