[英]Can I automatically remove a parameter on querystring with Request.Url.Query?
When I get the querystring with Request.Url.Query
I'd like to exclude a parameter (without using replace, or function like these). 当我使用
Request.Url.Query
获取查询字符串时,我想排除一个参数(不使用replace或类似的函数)。
So, for example, if I have this querystring : 因此,例如,如果我有这个querystring:
?ID=1241&IDL=241&HC=1241
I'd like to exclude IDL
getting : 我想排除
IDL
得到:
?ID=1241&HC=1241
Is this a implemented way or I need to make my own function? 这是一种实现的方式还是我需要行使自己的职能?
EDIT : I care about the order of my query string values. 编辑:我关心我查询字符串值的顺序。
It's still string manipulation, but how about something like: 它仍然是字符串操作,但是类似:
String.Concat("?",
String.Join("&",
Request.Url.Query.Substring(1)
.Split('&')
.Where(k => !k.StartsWith("IDL="))
.ToArray() // For .NET versions prior to v4.0
)
)
Alternatively you could use Request.QueryString
to get at a processed collection of query-string parameters. 或者,您可以使用
Request.QueryString
来获取已处理的查询字符串参数集合。
EDIT: This will leave your parameters in the order they were sent. 编辑:这将使您的参数按其发送顺序保留。
Here's a sample ASPX page that outputs a modified query-string (I've tested it in a ASP.NET 3.5 Web-Site): 这是一个示例ASPX页面,该页面输出经过修改的查询字符串(我已经在ASP.NET 3.5网站中对其进行了测试):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Linq" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Query String Removal</title>
</head>
<body>
Modified query-string: <%=
String.Concat("?",
String.Join("&",
Request.Url.Query.Substring(1)
.Split('&')
.Where(k => !k.StartsWith("IDL="))
.ToArray() // For .NET versions prior to v4.0
)
)
%>
</body>
</html>
NOTE: System.Linq
has been imported. 注意:
System.Linq
已导入。 An exception will be raised if you don't specify a query-string. 如果不指定查询字符串,则会引发异常。
I used this which can be called multiple times if you have a list of keys that you want removed from the querystring if they exists: 如果您有要从查询字符串中删除的键列表(如果存在),则可以多次调用它:
private string RemoveQueryStringItemWithKeyName(string url, string keyName)
{
var urlItemRemoved = String.Join("&", url.Split('&').Where(k => !k.StartsWith(keyName + "=")));
return urlItemRemoved;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.