I have the following C# Razor in my View:
@{var activeFilters = 0;}
@foreach (string key in Request.QueryString)
{
string value = Request.QueryString[key];
if (!String.IsNullOrEmpty(value))
{
if (key.ToLower() != "filter" || key.ToLower() != "page" || key.ToLower() != "gridtype")
{
activeFilters++;
}
}
}
This loops through the query string and counts have many have valid values to populate the activeFilters var. It also ignores filter, page and gridtype.
Here are some examples:
?page=1 (0)
?filter=&other=test (1)
?filter&other=test (1)
The problems I am having:
- It doesn't ignore the keys filter, page and gridtype
- It breaks if a query key has no equals e.g.
?page
with the errorObject reference not set to an instance of an object.