471,073 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Accessing HttpContext in ClassLibrary?

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
Feb 23 '07 #1
4 27509
HttpContext.Current

George

"Sami Rehman" <sa*********@4D.orgwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
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

Feb 23 '07 #2

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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:uW*************@TK2MSFTNGP05.phx.gbl...
HttpContext.Current

George

"Sami Rehman" <sa*********@4D.orgwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
>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

Feb 23 '07 #3
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]" <ka********@removeopenmymindremovemetoo.andmenetwr ote
in message news:37**********************************@microsof t.com...
>
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:uW*************@TK2MSFTNGP05.phx.gbl...
>HttpContext.Current

George

"Sami Rehman" <sa*********@4D.orgwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
>>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


Feb 26 '07 #4
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" <sa*********@4D.orgwrote in message
news:uV**************@TK2MSFTNGP06.phx.gbl...
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]" <ka********@removeopenmymindremovemetoo.andmenet >
wrote in message
news:37**********************************@microsof t.com...
>>
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:uW*************@TK2MSFTNGP05.phx.gbl...
>>HttpContext.Current

George

"Sami Rehman" <sa*********@4D.orgwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
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

Feb 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Iantosca | last post: by
2 posts views Thread by pc | last post: by
5 posts views Thread by coollzh | last post: by
reply views Thread by ABN | last post: by
5 posts views Thread by Victor Jones | last post: by
4 posts views Thread by Mat | last post: by
3 posts views Thread by Madhur | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.