【问题标题】:removing a query string from the request before processing it在处理请求之前从请求中删除查询字符串
【发布时间】:2020-01-29 04:36:35
【问题描述】:

我想删除一个特定的查询字符串(即 fbclid),Facebook 添加到共享链接以进行跟踪(我假设),以便我所有的工作路线,但我找不到直接的答案

我正在使用 asp net core 2.0、AWS lambda、api 网关

我尝试使用重写器,但是当替换为空字符串时它会抛出

app.UseRewriter(new RewriteOptions().AddRewrite("\\?fbclid=(\\w*)", string.Empty, false));

我不希望重定向到干净的 url 或在没有查询参数的情况下获取 url,我宁愿更改管道其余部分的当前请求以正确处理它

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-core asp.net-mvc-routing


    【解决方案1】:

    要删除查询字符串,您可以尝试Middleware like

    app.Use(async (context,next) =>
    {                
        if (context.Request.Query.TryGetValue("fbclid", out StringValues query))
        {
            var items = context.Request.Query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
            // At this point you can remove items if you want
            items.RemoveAll(x => x.Key == "fbclid"); // Remove all values for key
            var qb = new QueryBuilder(items);
            context.Request.QueryString = qb.ToQueryString();
        }
        await next.Invoke();
    });
    

    【讨论】:

    • 我(我认为还有成千上万的其他人)对 FB-param 有同样的问题 - 难以置信!您的答案将最能满足需求,因为它应该适用于任何网址。由于我已经实现了“中间件”,因此我已经对您的代码进行了测试并注意到了一个问题。如果发生,请查看 fbclid 参数并将其删除,但是浏览器中的 uri 没有更改 = 不喜欢的参数保留在浏览器 url 中。我假设,那 await next.invoke() 传递了 ORIGINAL url(带有参数)?也许你可以对此说点什么?
    猜你喜欢
    • 2014-09-06
    • 2022-08-19
    • 2013-04-06
    • 2016-09-03
    • 2010-10-07
    • 1970-01-01
    • 2018-08-11
    • 2020-03-01
    • 1970-01-01
    相关资源
    最近更新 更多