set default url param if not exist .net

34 views Asked by At

I'm a bit stuck on how to solve this.

The edge case I'm testing for is if someone goes to the URL but removes the query params.

EXAMPLE

www.example.com

I want to be able to check if there are no URL params given then set it to a default and redirect with the default URL params.

EXPECTED RESULT

www.example.com?year=2016

This is my code I'm using or misusing. Some help would be very much appreciated.

Dim noURLParam As String = ""
If Not(Request.QueryString("Year") Is Nothing) Then
  If Request.QueryString("Year").ToString() <> "" Then
    noURLParam = Request.QueryString("Year").ToString()
  End If
End If
1

There are 1 answers

1
Jamiec On

The way your code is currently structured you can simply do this

' Set a default
Dim noURLParam As String = "2017" 

And 2017 will be the default if no value is provided for year in the querystring.