5

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?

Yannik
  • 887
  • 3
  • 9
  • 29
  • What do you mean by "can't use" exactly? What is your error message? If you are using Visual Studio, you can let VS create missing using statements by selecting the line with the error, pressing ctrl+. and selecting the appropriate option. – Tim Pohlmann Jul 13 '16 at 09:31
  • @TimPohlmann I found some tutorials on stackoverflow. In this tutorial they used the HttpCookie statement. I tried to implement this in my own project. VB2015 tell me this: “the type or namespace HttpCookie could not be found....” You’re right VB give in normal cases some “to does” but in this case VB give no “to does” The HttpContext-method can I already use because I follow the “to does” of VB. Is this because I use the latest .NET Core? – Yannik Jul 13 '16 at 09:51
  • VB? I assume you mean VS (aka Visual Studio). Weird that it does not propose the usings. – Tim Pohlmann Jul 13 '16 at 09:54
  • @TimPohlmann sorry my mistake :D i mean VS for Visual Studio :) – Yannik Jul 13 '16 at 10:28
  • 2
    If you are using ASP.Net MVC then beware that the HttpContext is under the Controller section as the page life cycle is changed using that framework. https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext%28v=vs.118%29.aspx?f=255&MSPPError=-2147217396 may be useful for more details. – JB King Jul 13 '16 at 11:28
  • @JBKing thank you for your answer. I found [this](http://senvichet.com/blog/how-to-create-get-asp-net-core-cookie/#) and I tried it but it isn’t work  C# don’t know the `CookieOption`-Method. Back to your answer: C# don’t know `System.Web` and for this reason `System.Web.mvc’ too. – Yannik Jul 13 '16 at 12:18

2 Answers2

4

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;
Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61
  • As per @JB King's comment on the OP, the `HttpContext` will be in the scope of a controller. So, although `using System.Web` will give you access to the `HttpContext` object, it will not be in the same scope as the rest of the app - as the `HttpContext` is in the scope of the Controllers – Geoff James Jul 13 '16 at 12:11
  • @GeoffJames watch my comment – Yannik Jul 13 '16 at 12:20
  • @Yannikhere I don't know why the assembly is missing for you, sorry. – Tim Pohlmann Jul 13 '16 at 12:31
  • @TimPohlmann ;( I created a normal ASP.NET Core webapplication lol. Can you open my project an try if it work by you, please [github]( https://github.com/YannikG/eCoffee/tree/master/eCoffee) IIf you done give me please a feedback… I will commit the latest version wait... – Yannik Jul 13 '16 at 12:40
  • @Yannikhere My guess is, it has something to do with the project being an ASP.NET Core webapplication. I'm not familiar with those. In a regular C# project my solution works just fine. – Tim Pohlmann Jul 13 '16 at 13:25
  • @TimPohlmann and I'm a IT-trainee :) ok no problem I think i found [something](http://www.mikesdotnetting.com/article/270/sessions-in-asp-net-core-1-0) – Yannik Jul 13 '16 at 14:30
-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

Yannik
  • 887
  • 3
  • 9
  • 29
  • 3
    That article is for implementing Session. But if you want to implement cookie handling I recommend to use the Microsoft.AspNetCore.Http namespace and the following classes Microsoft.AspNetCore.Http.HttpResponse, Microsoft.AspNetCore.Http.HttpRequest and Microsoft.AspNetCore.Http.CookieOptions – El Bayames Apr 06 '17 at 14:45