I have a ASP.NET Core razor page and model that uses query strings to filter search results and paging via GET.
Consider a user performed a search to arrive at this page:
https://localhost/Users?searchBy=Name&searchTerm=test&resultPage=2
Now on the result page I have a button that performs deletion via a popup modal. When the user clicks delete, it POSTS to a page handler OnPostDelete()
. I use the tag helper asp-page-handler
for this:
<button asp-page-handler="Delete" asp-route-id="@Model...">
After the deletion, I want the user to be redirected back to the exact same URL as before, so he/she stays on the same result page, instead of being redirected to plain index page.
So my question is how to easily keep these query strings attached throughout the process of posts and redirects? Currently here's what I'm doing:
On the deletion popup modal razor page, I use these codes to capture the current query strings: (I can't figure out how to directly convert Context.Request.Query
to a Dictionary
)
@{
Dictionary<string, string> query = new Dictionary<string, string>();
foreach (var queryString in Context.Request.Query)
{
query.Add(queryString.Key, queryString.Value);
}
}
I then add the query to the asp-all-route-data
tag helper on the delete button:
<button asp-page-handler="Delete" asp-all-route-data="query" asp-route-id="@Model...">
This makes the resulting formaction
attribute generated by the tag helper to contain the query strings in the POST URL.
Then in my page handler's OnPostDelete()
method, I re-capture the query strings again, removing the undesirable ones (id etc) added by the delete button, before redirecting:
Dictionary<string, string> query = new Dictionary<string, string>();
foreach (var queryString in HttpContext.Request.Query)
{
if ((queryString.Key != "id") && (queryString.Key != "handler"))
{
query.Add(queryString.Key, queryString.Value);
}
}
return RedirectToPage($"Index",query);
It works but seems very tedious. I'm new at this and I'm sure there's a better way to do this. Your help would be greatly appreciated.