Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Silvija 58 posts 172 karma points
    Oct 01, 2019 @ 10:18
    Silvija
    0

    Request Cookies Null reference

    Hi All,

    Its not Umbraco related question but this community is so frendly that i will ask here.

    I want to save cookie every time visitor comes to my website and before I do that I want to check if cookie alredy exist.

    I am doing this with this static method:

    public bool IsReturningVisitor(string value)
            {
                if (Request.Cookies[value] != null)
                {
                    return true;
                }
                else
                {
                    Response.Cookies[value].Value = value;
                    Response.Cookies[value].Expires = DateTime.Now.AddMinutes(1);
                    return false;
                }
            }
    

    But I am geting null reference on this line Request.Cookies[value] != null . When i do the same thing in my ActionResult class it works good.

    What am I missing??

  • Malthe Petersen 68 posts 383 karma points c-trib
    Oct 02, 2019 @ 18:02
    Malthe Petersen
    0

    Hi Silvija.

    Are you sure you have a Request? If you do it in a static class with a static method you might not have the Request object available.

    If you have a Request object, check to see what the Cookies property actually contains, maybe what you think your key is, is not what it actually is.

    Regards Malthe

  • Silvija 58 posts 172 karma points
    Oct 09, 2019 @ 10:00
    Silvija
    0

    Hi Malthe,

    Yes that is true, Request is null. I cant find solution how to solve it. Is there a way to get the cookies in that situation?

    BR

    Silvija

  • Malthe Petersen 68 posts 383 karma points c-trib
    Oct 12, 2019 @ 11:38
    Malthe Petersen
    0

    Hi again.

    Yes. You can get the Request/Response object from HttpContext.Current.Request/Response.

    Regards

Please Sign in or register to post replies

Write your reply to:

Draft