1

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 error Object reference not set to an instance of an object.
Cameron
  • 27,963
  • 100
  • 281
  • 483
  • 2
    Why are you doing this in Razor/View and not in Controller and sending simple count down to view? IMHO the view should be as simple as possible and not contain lots of code / logic. – Belogix Nov 12 '13 at 11:33
  • Because it's used in multiple places and it's easier to do it in the view once. – Cameron Nov 12 '13 at 11:33
  • 1
    @Cameron - you can resolve that at the Controller side too. – H H Nov 12 '13 at 11:36
  • In any case the problem still exists in the code. – Cameron Nov 12 '13 at 11:36

2 Answers2

4

If you want to ignore the keys filter, page and gridtype you have to change

if (key.ToLower() != "filter" || key.ToLower() != "page" || key.ToLower() != "gridtype")

to

if (key.ToLower() != "filter" && key.ToLower() != "page" && key.ToLower() != "gridtype")

You don't want the key to be "filter" and you don't want it to be "page" and "gridtype" either.

As for the problem with missing equal sign put a condition at the top of the loop

if (key == null) continue; 

It will skip null keys. Also, replace

@foreach (string key in Request.QueryString)

with

@foreach (string key in Request.QueryString.AllKeys)
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • With it being inside a loop I assumed it would check as an OR but then realised it might only loop just the once if only one query string key exists. What about the problem with no equals on the query string key? – Cameron Nov 12 '13 at 11:44
  • @Cameron That part I don't fully understand. Try to exaplain it better, please. – Ondrej Janacek Nov 12 '13 at 11:47
  • If I have the following query string: `domain.com/?page=1&param` It errors with: `Object reference not set to an instance of an object.` But if I have: `domain.com/?page=1&param=` it works. So it breaks if a key doesn't have an equals following it, regardless if the value is null or not. – Cameron Nov 12 '13 at 11:48
  • This seems to talk about it: http://stackoverflow.com/questions/9621562/asp-net-querystring-without-equals-sign but I'm not sure how I'd deal with it in my code to stop it erring. Thanks. – Cameron Nov 12 '13 at 12:02
  • I updated my answer. Does it solve your problem? If there isn't an equal sign then it should not be recognized as a key. – Ondrej Janacek Nov 12 '13 at 12:15
  • Still get the error. It errors on the `if(key.ToLower...` line. – Cameron Nov 12 '13 at 12:21
  • Put a condition at the top of the loop `if (key == null) continue;` It will skip null keys. – Ondrej Janacek Nov 12 '13 at 12:23
2

You can easily,

@{ var activeFilters = Request.QueryString.AllKeys
       .ToList()
       .Where(key => key != "filter")
       .Where(key => key != "page")
       .Where(key => key != "gridtype")
       .Count(key => !string.IsNullOrEmpty(Request.QueryString[key]));}
mehmet mecek
  • 2,615
  • 2
  • 21
  • 25