Search code examples
cookiesasp.net-mvc-4httpcontext

Is there any difference between HttpContext.Current.Request and HttpContext.Request?


If I want to access Request object in controller action I need to write HttpContext.Request, whereas if I want to access the same object in MVC view, I need to write HttpContext.Current.Request. Is there any difference between them? The problem I am facing is that, the cookies which I set through HttpContext.Response.Cookies.Add in controller action are not being retrieved in HttpContext.Current.Request.Cookies collection in an MVC view, though I can see those cookies through javascript.


Solution

  • The reason you have to write HttpContext.Request in a controller and HttpContext.Current.Request on a view is because a controller that you write inherits the abstract class Controller that has a property called HttpContext that is of type HttpContextBase. The view then uses the sealed class HttpContext that gives you an httpcontext object for the current request.

    Is there any difference between them?

    No. As both give you the same HttpRequest object for the current request.