Reputation: 2135
I am writing a middleware where I want to modify query string values for the current request but I am unable to do that. afaik QueryString.Add
method should work but it doesn't affect the query string. Here's what I tried.
public async Task Invoke(HttpContext context, IHeaderValue headerValue, IUser user)
{
var result = context.Request.Headers["Authorization"];
if (result.Count != 0)
{
headerValue.UserId = this.GetUserIdFromToken(result[0]);
var request = context.Request;
if (request.Method == "GET")
{
// It should be adding the new query value to the QueryString collection
// but it doesn't
request.QueryString.Add("CurrentUserId", headerValue.UserId.ToString());
}
}
}
I will really appreciate any help with this.
Upvotes: 9
Views: 4287
Reputation: 42330
QueryString.Add
returns a new QueryString
containing the given name and value. It doesn't mutate the QueryString
on which it's called.
So you need to do something like
request.QueryString = request.QueryString.Add("A", "B");
Upvotes: 13