Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't use HttpContext or HttpCookie? (Asp.Net Core 1.0)

Why can't I use HttpContext or HttpCookie? Is there a special using?

My actual usings:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

My namespace:

namespace eCoffee.Main

My class + methods:

public class AppRunCookie
{
    public static string CookieName { get; set; }
    public static string Key { get; set; }
public AppRunCookie(string key)
{
    CookieName = "MyCookie_" + key;
}

public static void SetCookie(string key, int cookieExpireDate = 30)
{
    HttpCookie myCookie = new HttpCookie(CookieName);
    myCookie["Key"] = key;
    myCookie.Expires = DateTime.Now.AddDays(cookieExpireDate);
    HttpContext.Current.Response.Cookies.Add(myCookie);
}

public string GetCookie()
{
    HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName];
    if (myCookie != null)
    {
        string key = Convert.ToString(myCookie.Values["Key"]);
        return key;
    }
    return "";
}
}

What am I do wrong?

like image 596
Yannik Avatar asked Oct 31 '22 00:10

Yannik


1 Answers

The namespace for HttpContext and HttpCookie is System.Web which is part of the System.Web.dll library.

Go right click the projects References and select Add References.... In the newly opened window click on Assemblies and then search (via the search bar in the upper right) for System.Web.

Then you should be able to use it via

using System.Web;
like image 130
Tim Pohlmann Avatar answered Nov 10 '22 00:11

Tim Pohlmann