Search code examples
c#asp.netquery-stringquerystringparameter

Asp.net: URL with Query string while loaded in new tab throws object reference not set to instance error


abc.aspx is my page and in my site menu navigation I have categories like "apple", banana" etc. While clicking the categories in the menu I'm appending the categories as query string in the URL like below

http://www.example.com/abc/abc.aspx?filter=apple

Page works fine when user clicks the menu and loads this page. However, if we load this URL separately in new tab/new window it throws "Object reference not set to instance" error. This issue is not reproduced in local.

string filter = Request.QueryString["filter"]; 
Data[] dt_Data = Search.GetResults(filter);

This code just pass the query string value to GetResults method and returns array of Data objects which i bind to the page. Please note that the code is working fine while navigating via Menu. If the page is loaded in a new tab/new window then only I'm getting error. So here code implementation doesn't seem to have any issue.


Solution

  • The problem is, I have logs in which the UrlReferrer is captured something like below

    Uri myReferrer = Request.UrlReferrer;
    UrlRefer = myReferrer.ToString();
    

    When I click the Category from Menu then URL refer is www.example.com. But when I load the page in a seperate window/tab this UrlReferrer becomes null which is thrown as error.