Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing HttpContext in a class library - ASP.NET MVC

I am populating an instance of a class that represents the session variable and exists in a class library. I need to populate one property that requires me to get access to the Request object. I understand that I can use the System.Web.HttpContext.Current.Request to get the request object?

Is this a good practice. Something tells me that I should be doing this in the controller (or the base controller) that has the System.Web namespace.

like image 353
DotnetDude Avatar asked May 11 '11 18:05

DotnetDude


People also ask

Where is HttpContext items stored?

It is stored in the memory of the server and the value is available for the entire lifetime of the request.

What is the difference between session and HttpContext current session?

There is no difference. The getter for Page. Session returns the context session. Save this answer.

What is the difference between HttpContext current items and HttpContext current session in asp net?

Current. Item” data is live for single HTTP request/Response where HttpContext. Current. Session data is live throughout user's session.


1 Answers

Its not a bad practice - the object is available - if you design dictates to access it there - then by all means do it. One option is to pass in an instance of the request to the class's constructor if it makes you feel cleaner : )

like image 67
Adam Tuliper Avatar answered Oct 20 '22 16:10

Adam Tuliper