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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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;
Method 2
So Hello guys
I found a solution, try to use this wonderful blog entry.
Note:
Make sure you install the nuget-packages via the nuget installer and make also sure you checked the checkbox “include prerelease”.
Hope my poste was helpful
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0