0

I am having an issue with testing my asp.NET website. lets say the form is defualt.aspx. When I try to open the form it gives me this weird error while trying to request a cookie value example:

     cookieVal = Request.Cookies["cookie"].Value;

The error message says:

    An exception of type 'System.Null.ReferenceException' occured in
    App_Web_default.aspx.dfa151d5.jahcut42.dll but was not handled in user code.

Is there anything I can do to fix this issue? I am very lost as I never had this error before. Thank you!

1 Answers1

1

The error is clearly stating that there is a null reference in the code, i.e, there is no such key called "cookie" in Request.Cookies. And if it is not there, then then object is null and you can't extract a Value property out of it. The correct way to handle this :

cookieVal = Request.Cookies["cookie"] != null ? Request.Cookies["cookie"].Value : null;

OR you can check if they key = "cookie" exists,

cookieVal = Request.Cookies.ContainsKey("cookie") ? Request.Cookies["cookie"].Value : null;
DinoMyte
  • 8,367
  • 1
  • 15
  • 23