Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access to HttpContext.Current [duplicate]

I can't access to HttpContext.Current on my project MVC4 with C#4.5

I've added my reference to System.Web in my project and added the using instruction on my controller page...

But I can access currentHandler only...

var context = HttpContext.CurrentHandler; //Current 

Is HttpContext.Current deprecated on C#4.5 ?

I've looked this help page : http://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx

like image 409
Ema.H Avatar asked Aug 19 '13 08:08

Ema.H


People also ask

What can I use instead of HttpContext current?

Instead of using HttpContext. Current , use the HttpContext provided as a property on the Page or Controller , or even better, you can simply use the Session property.

How do I find HttpContext current?

HTTP context accessor. Finally, you can use the IHttpContextAccessor helper service to get the HTTP context in any class that is managed by the ASP.NET Core dependency injection system. This is useful when you have a common service that is used by your controllers.

How use HttpContext current in .NET Core?

Current property, instead it is available in the HttpContext class in ASP.Net Core applications. Session can be enabled using the Configure method. Inside this method, you will have to call the UseSession method of the app object. Note: It is mandatory to call the UseSession method before the UseMvc method.


2 Answers

Have you included the System.Web assembly in the application?

using System.Web; 

If not, try specifying the System.Web namespace, for example:

 System.Web.HttpContext.Current 
like image 133
Darren Avatar answered Sep 20 '22 07:09

Darren


This is because you are referring to property of controller named HttpContext. To access the current context use full class name:

System.Web.HttpContext.Current 

However this is highly not recommended to access context like this in ASP.NET MVC, so yes, you can think of System.Web.HttpContext.Current as being deprecated inside ASP.NET MVC. The correct way to access current context is

this.ControllerContext.HttpContext 

or if you are inside a Controller, just use member

this.HttpContext 
like image 27
Andrei Avatar answered Sep 23 '22 07:09

Andrei