31

I have a quite simple problem. I want to create a cookie at a Client, that is created by the server. I've found a lot of pages that describe, how to use it - but I always stuck at the same point.

I have a DBController that gets invoked when there is a request to the DB.

The DBController's constructor is like this:

public class DBController : Controller
{
    public DBController()
    {
        HttpCookie StudentCookies = new HttpCookie("StudentCookies");
        StudentCookies.Value = "hallo";
        StudentCookies.Expires = DateTime.Now.AddHours(1);
        Response.Cookies.Add(StudentCookies);
        Response.Flush();
    }

    [... more code ...]

}

I get the Error "Object reference not set to an instance of an object" at:

StudentCookies.Expire = DateTime.Now.AddHours(1);

This is a kind of a basic error message. So what kind of basic thing I've forgot?

2
  • 2
    I suspect that Response is null in the controller constructor. It get's set later on. Try your code in an action method. Commented Sep 8, 2016 at 11:58
  • 1
    Ya @Jim it works inside action method Commented Sep 8, 2016 at 12:11

4 Answers 4

46

The problem is you cannot add to the response in constructor of the controller. The Response object has not been created, so it is getting a null reference, try adding a method for adding the cookie and calling it in the action method. Like so:

private HttpCookie CreateStudentCookie()
{
    HttpCookie StudentCookies = new HttpCookie("StudentCookies");
    StudentCookies.Value = "hallo";
    StudentCookies.Expires = DateTime.Now.AddHours(1);
    return StudentCookies;
}

//some action method
Response.Cookies.Add(CreateStudentCookie());
Sign up to request clarification or add additional context in comments.

Comments

15

Use Response.SetCookie(), because Response.Cookie.Add() can add multiple cookies, whereas SetCookie() will update an existing cookie. So I think your problem can be solved.

public DBController()
{
    HttpCookie StudentCookies = new HttpCookie("StudentCookies");
    StudentCookies.Value = "hallo";
    StudentCookies.Expires = DateTime.Now.AddHours(1);
    Response.SetCookie(StudentCookies);
    Response.Flush();
}

2 Comments

Using your method too 'System.NullReferenceException' occurs at line Response.SetCookie(StudentCookies);
In reference to Jim's comment, your provided code doesn't work
2

Use

Response.Cookies["StudentCookies"].Value = "hallo";

to update existing cookie.

1 Comment

It looks like in .Net core is now: Response.Cookies.Append("StudentCookies", "hallo");
1

You could use the Initialize() method of the controller instead of the constructor. In the initialize function the Request object is available. I suspect that the same action can be taken with the Responseobject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.