.NET 中获取url参数,多条件查询拼接QueryString并选中当前条件
在ASP.NET里面获取URL参数如下
@Html.ViewContext.RouteData.Values["id"]
@Request.RequestContext.RouteData.Values["id"]
@Html.ViewContext.RouteData.Route.GetRouteData(Html.ViewContext.HttpContext).Values["id"]
ASP.NET Core里面获取URL参数如下
@Context.Request.Query["Name"]
.NetCore
前端调用
<a class="btn btn-primary @HttpRequestExtensions.GetActive(ViewContext.HttpContext.Request.QueryString.Value,"sort","")" target="_self" href="/AllResourses/List?@(Html.Raw(HttpRequestExtensions.UpdateQueryString(ViewContext.HttpContext.Request.QueryString.Value, "sort","")))"> 最新上传 </a> <a class="btn btn-primary @HttpRequestExtensions.GetActive(ViewContext.HttpContext.Request.QueryString.Value,"sort","1")" target="_self" href="/AllResourses/List?@(Html.Raw(HttpRequestExtensions.UpdateQueryString(ViewContext.HttpContext.Request.QueryString.Value, "sort","1")))"> 最多下载 </a> <a class="btn btn-primary @HttpRequestExtensions.GetActive(ViewContext.HttpContext.Request.QueryString.Value,"sort","2")" target="_self" href="/AllResourses/List?@(Html.Raw(HttpRequestExtensions.UpdateQueryString(ViewContext.HttpContext.Request.QueryString.Value, "sort","2")))"> 评分最高 </a>
后台
public static class HttpRequestExtensions { public static string RemoveQueryString(string queryString, string name) { var dic = GetFormData(queryString); dic.Remove(name); return ToQueryString(dic); } public static string UpdateQueryString(string queryString, string name, string value) { var dic = GetFormData(queryString); if (dic.ContainsKey(name)) { dic[name] = value; } else { dic.Add(name, value); } return ToQueryString(dic); } public static string UpdateQueryStringceshi(string queryString, string []name, string []value) { var dic = GetFormData(queryString); for (int i = 0; i < name.Length; i++) { if (dic.ContainsKey(name[i])) { dic[name[i]] = value[i]; } else { dic.Add(name[i], value[i]); } } return ToQueryString(dic); } /// <summary> /// 将获取的formData存入字典数组 /// </summary> public static Dictionary<String, String> GetFormData(string formData) { try { if (formData.IndexOf("?", StringComparison.Ordinal)!=-1) { formData = StringHelper.GetRight(formData, "?"); } //将参数存入字符数组 String[] dataArry = formData.Split('&'); //定义字典,将参数按照键值对存入字典中 Dictionary<String, String> dataDic = new Dictionary<string, string>(); //遍历字符数组 for (int i = 0; i <= dataArry.Length - 1; i++) { //当前参数值 String dataParm = dataArry[i]; //"="的索引值 int dIndex = dataParm.IndexOf("="); if (dIndex > 0) { //参数名作为key String key = dataParm.Substring(0, dIndex); //参数值作为Value String value = dataParm.Substring(dIndex + 1, dataParm.Length - dIndex - 1); //将编码后的Value解码 String deValue = System.Web.HttpUtility.UrlDecode(value, System.Text.Encoding.GetEncoding("utf-8")); //将参数以键值对存入字典 dataDic.Add(key, deValue); } } return dataDic; } catch (Exception ex) { throw new Exception(ex.Message); } } public static string ToQueryString(Dictionary<String, String> dic) { StringBuilder sb = new StringBuilder(); foreach (var key in dic) { //EscapeDataString就是对特殊字符进行uri编码 sb.Append(key.Key).Append("=") .Append(Uri.EscapeDataString(key.Value)).Append("&"); } return sb.ToString().Trim('&');//去掉最后一个多余的& } /// <summary> /// /// </summary> /// <param name="queryString">Url</param> /// <param name="name">参数</param> /// <param name="value">参数值</param> /// <param name="Default">默认值</param> /// <param name="returnValue">返回字符串</param> /// <returns></returns> public static string GetActive(string queryString, string name,string value,string Default = "",string returnValue= "active") { var dic = GetFormData(queryString); //包含name if (dic.ContainsKey(name)) { if (value == dic[name]) { return returnValue; } return ""; } //没有name,如果等于默认值 if (value== Default) { return returnValue; } return ""; } public static string GetActive(string viebag, string value) { if (viebag== value) { return "active"; } return ""; } }
ASP.NET MVC5中QueryString拼接更新
后台
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);
}
前端
<div class="menu-list clearfloat am-sharetwo">
<ul class="yiji" id="oe_menu">
<li>
<a href="#" class="inactive">区域<i></i></a>
<ul style="display: none">
<li><a href="~/House/Search?@(MVCHelper.RemoveQueryString(Request.QueryString,"RegionId"))">全部</a></li>
@foreach (var region in Model.regions)
{
<li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"RegionId",region.Id.ToString()))">@region.Name</a></li>
}
</ul>
</li>
<li>
<a href="#" class="inactive">租金<i></i></a>
<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>
</li>
<li>
<a href="#" class="inactive">排序<i></i></a>
<ul style="display: none">
<li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentAsc"))">租金从低到高</a></li>
<li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentDesc"))">租金从高到低</a></li>
<li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","AreaAsc"))">面积从小到大</a