简体   繁体   English

我可以使用Request.Url.Query自动删除querystring上的参数吗?

[英]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.

相关问题 Request.Url.Query和Request.QueryString有什么区别? - What's the difference between Request.Url.Query and Request.QueryString? 替换规范URL中的Request.Url.Query值 - Replace Request.Url.Query value in Canonical URL 访问Request.QueryString [foo]为空,但URL显示查询字符串/参数就在那 - Accessing Request.QueryString[foo] is null but the URL shows the query string/parameter is right there Request.Querystring自动url解码字符串? - does Request.Querystring automatically url decode a string? 如何加密 id 查询字符串参数 - How can I encrypt the id querystring parameter C#获取URL查询字符串Request.QueryString不起作用 - C# get URL query string Request.QueryString not working Url跳过HttpContext.Current.Request.Url.AbsoluteUri中的第二个查询字符串参数 - Url skip second querystring parameter in HttpContext.Current.Request.Url.AbsoluteUri 如何使用 MVC 隐藏 url 中的请求参数字段? - How can i hide the Request parameter fields in url using MVC? 我可以使用NavigationContext.QueryString设置参数吗? (Windows Phone) - Can I set parameter using NavigationContext.QueryString? (Windows Phone) 加载视图后,如何删除(或隐藏)查询字符串? - How can I remove (or hide) the querystring after a View loads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM