Accessing HttpContext in ClassLibrary?

  • Thread starter Thread starter Sami Rehman
  • Start date Start date
S

Sami Rehman

hi
i have a vs solution in which there are 2 projects
class library representing Security services and another one is web
application project.
i need to access http context in a static method in a class of Security
Services project.
how can this be done.

thanks
-sami
 
which should be avoided when possible, but obviously isn't always.

at the very least you could do a null check:

if (HttpContext.Current == null)
{
//either fallback to something or throw some exception
//throw new NotImplementedException("This function can only be called
within a webrequest-context for now");
}


Don't remember if you need to reference System.Web.dll, I'd assume so.

Karl
 
but the problem is i have got MyCompany.MyProject.Security.dll reference in
MyCompany.MyProject.WebUI.dll which is the right way i guess because
Security's services will be consumed by WebUI..... it is however in Security
that i need to access the session object.

the error that i get at compile time is that 'The name 'HttpContext'' does
not exist in the current context", which is true since Security is another
project.

can i access the httpcontext object without passing it as an argument to a
class in Security component??
 
Yes, and george told you how..you simply need to do

System.Web.HttpContext current = System.Web.HttpContext.Current;

you'll need to add a reference to System.Web as well, but that shouldn't
cause any problems.

--
http://www.openmymind.net/
http://www.fuelindustries.com/


Sami Rehman said:
but the problem is i have got MyCompany.MyProject.Security.dll reference
in MyCompany.MyProject.WebUI.dll which is the right way i guess because
Security's services will be consumed by WebUI..... it is however in
Security that i need to access the session object.

the error that i get at compile time is that 'The name 'HttpContext'' does
not exist in the current context", which is true since Security is another
project.

can i access the httpcontext object without passing it as an argument to a
class in Security component??




"Karl Seguin [MVP]" <[email protected]>
wrote in message
 
Back
Top