I would like to remove a particular query string (namely fbclid
), which Facebook adds to shared links for tracking (I assume). I need to do this for all of my routes to work, but I couldn't find a straightforward solution.
I'm using ASP.NET Core 2.0, AWS lambda, API gateway.
I've tried to use a rewriter, but it throws an exception when the replacement is an empty string. Here is my code:
app.UseRewriter(new RewriteOptions()
.AddRewrite("\\?fbclid=(\\w*)", string.Empty, false));
I do not wish to redirect to a clean URL or get the URL without the query param. Rather, I need to change the current request for the rest of the pipeline to process it correctly.
For removing the query string, you could try 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();
});