【问题标题】:How to remove or update part of query string in asp.net如何在 asp.net 中删除或更新部分查询字符串
【发布时间】:2013-02-09 16:56:41
【问题描述】:

我有以下查询字符串,我想删除或更新部分查询字符串

?language=en-us&issue=5&pageid=18&search=hawaii&search=hawaii // 这是我需要格式化的内容

问题是我的搜索键有重复的查询字符串。

我尝试了以下代码,但它不起作用

public static string RemoveQueryStringByKey(string url, string key)
{
    var uri = new Uri(url);

    // this gets all the query string key value pairs as a collection
    var newQueryString = HttpUtility.ParseQueryString(uri.Query);

    // this removes the key if exists
    newQueryString.Remove(key);

    // this gets the page path from root without QueryString
    string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);

    return newQueryString.Count > 0
        ? "{0}?{1}".FormatWith(pagePathWithoutQueryString, newQueryString)
        : pagePathWithoutQueryString;
}

FormatWith 产生错误

string' does not contain a definition for 'FormatWith' and no extension method 'FormatWith' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?

工作代码:

public static string RemoveQueryStringByKey(string url, string key)
{
    var uri = new Uri(url);

    // this gets all the query string key value pairs as a collection
    var newQueryString = HttpUtility.ParseQueryString(uri.Query);

    // this removes the key if exists
    newQueryString.Remove(key);

    // this gets the page path from root without QueryString
    string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);

    return newQueryString.Count > 0
        ? string.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString)
        : pagePathWithoutQueryString;
}

函数调用

var URL = Request.Url.AbsoluteUri;
var uri = new Uri(Helper.RemoveQueryStringByKey(URL, "search"));

在我的实际逻辑中,我必须检查 QueryString search 是否存在,如果存在则我将其删除并用新的搜索关键字替换它。

您可以按照逻辑应用它,并且第一个示例代码不起作用是由于 FormatWith 我无法修复,所以我使用了 Oded 提供的解决方案,它对我有用。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    直接使用string.Format。看起来您从定义了 FormatWith 扩展方法的代码库中复制了代码,但您尚未复制(或未包含它所在的命名空间)。

    使用string.Format

    return newQueryString.Count > 0
        ? string.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString)
        : pagePathWithoutQueryString;
    

    如果扩展方法在代码库中,另一种选择是在扩展方法所在的命名空间中添加using declaration

    【讨论】:

    • 你说得对,我从 stackoverflow.com/questions/11052744/… 获取了这段代码,我无法找到一个完整的例子
    • @KnowledgeSeeker - 我给了你一段不同的代码,应该可以正常工作。
    • 我尝试了第一个代码它会生成错误The format of the URI could not be determined 我会尝试更新一个。只是为了更清楚,我没有传递所有的 URI,我只需要传递查询字符串部分,就像我展示的那样例如
    • @KnowledgeSeeker - 是时候破解打开调试器了。查看您尝试连接的值是什么。
    • 这就是我的部分代码问题是 URI 转换QueryString = Request.Url.Query.ToString(); Helper.RemoveQueryStringByKey(QueryString,"search"); Helper.RemoveQueryStringByKey("?language=en-us&issue=5&pageid=18&search=hawaii", "search")
    【解决方案2】:
    return newQueryString.Count > 0
            ? "{0}?{1}".FormatWith(pagePathWithoutQueryString, newQueryString)
            : pagePathWithoutQueryString;
    

    string' 不包含 'FormatWith' 的定义,并且找不到接受“string”类型的第一个参数的扩展方法 'FormatWith'(您是否缺少 using 指令或程序集引用?

    我认为该错误是不言自明的,您的返回代码中存在错误。
    字符串对象中没有 FormatWith。


    QueryStrings 也可以用Request.QueryString 收集,这将返回一个键和值的集合。 更改此集合,然后使用 Response.QueryString 重新发送它可能会满足您的要求。

    【讨论】:

    • 这究竟是如何帮助 OP 或回答问题的?
    • @Oded OP应该知道错误出在他自己的代码上,而不是在未编译的代码上
    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    相关资源
    最近更新 更多