2

I am new to ASP.NET and trying to add a new cookie. I am using ASP.NET version 3.1.401 and in my homecontroller file I am trying to follow this: How to create persistent cookies in asp.net?. I have a using-statement using System.Web and within my homecontroller class I have a method which looks like:

public IActionResult Index()
{
    @ViewData["timezone"] = Convert.ToString(TimeZoneController.showTimeZone());
    @ViewData["ip"] = IPController.getIP();
    //create a cookie
    HttpCookie myCookie = new HttpCookie("myCookie");

    //Add key-values in the cookie
    myCookie.Values.Add("userid", "new_user");

    //set cookie expiry date-time. Made it to last for next 12 hours.
    myCookie.Expires = DateTime.Now.AddHours(12);

    //Most important, write the cookie to client.
    Response.Cookies.Add(myCookie);
    return View();
}

And I keep getting the following error:

CS1729: 'HomeController.HttpCookie' does not contain a constructor that takes 1 arguments

And

CS1061: 'HomeController.HttpCookie' does not contain a definition for 'Values' and no accessible extension method 'Values' accepting a first argument of type 'HomeController.HttpCookie' could be found (are you missing a using directive or an assembly reference?)

How can I get and set cookies and where should I do it within the MVC pattern?

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
bubu
  • 179
  • 2
  • 13
  • Do you have a custom type named `HttpCookie` defined in your project? From the error messages, you're referencing a `HomeController.HttpCookie` instead of `System.Web.HttpCookie` – devNull Aug 24 '20 at 02:59
  • There is no "ASP.NET version 3.1.401" - do you **really** mean ASP.NET **Core** v3.1.401 ?? If so - please clarify! You need to be **precise** with your labelling of products and versions ! – marc_s Aug 24 '20 at 05:34
  • Can you try this: HttpContext.Request.Cookies.Values["userid"]= "SomeValue"; – Rohan Rao Aug 24 '20 at 09:02
  • That doesn't address the initial error I get on this line `HttpCookie myCookie = new HttpCookie("myCookie");`, so there is still an error when I try that – bubu Aug 24 '20 at 15:10

2 Answers2

1

I realized that my mistake was that I am developing on macos and as such I am using ASP.NET core NOT ASP.NET. It is a silly mistake, but I'm sure many beginners will make it. Here is a link on how to set cookies when you are using ASP.NET core.

bubu
  • 179
  • 2
  • 13
-1

Have you tried:

        //Add key-values in the cookie
        myCookie.Values["userid"] = "new_user";

That should work. Also make sure you're using HttpCookie from System.Web.

Umang
  • 815
  • 5
  • 17
  • In that case I get the following error `CS1061: 'HomeController.HttpCookie' does not contain a definition for 'Values' and no accessible extension method 'Values' accepting a first argument of type 'HomeController.HttpCookie' could be found (are you missing a using directive or an assembly reference?)` – bubu Aug 24 '20 at 03:07
  • Ok. Try `System.Web.HttpCookie myCookie = new System.Web.HttpCookie("myCookie");` – Umang Aug 24 '20 at 03:18
  • Then I get the following error `CS0234: The type or namespace name 'HttpCookie' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)` – bubu Aug 24 '20 at 03:29
  • close visual studio and reopen it. Do a clean build after that – Umang Aug 24 '20 at 03:37