How do I access 'Request' from a Library?

G

Guest

In one of my business objects I wanted to include a method like this:

public static void PageRedirect(string pageName)
{
Response.Redirect(Request.ApplicationPath + "/" + pageName);
}

The class is referencing 'System.Web' but yet I'm getting a compile error in
regard to "Request". How come this method works if sitting in the aspx.cs
file but not in the cs file in my business object?
 
T

tedqn

yep, encountered this problem yesterday. If not executed directly as a
page, need to use.

Imports System.Web

HttpContext.Current.Request.ApplicationPath

HttpContext.Current.Response.Redirect
 
J

Joerg Jooss

Thus wrote Robert W.,
In one of my business objects I wanted to include a method like this:

public static void PageRedirect(string pageName)
{
Response.Redirect(Request.ApplicationPath + "/" + pageName);
}
The class is referencing 'System.Web' but yet I'm getting a compile
error in regard to "Request". How come this method works if sitting
in the aspx.cs file but not in the cs file in my business object?

The reason you can easily access Request, Response and other intrinsic objects
is that these are properties of the Page class. If you want to use them in
a library, you'll have to pass them as method parameters.

Cheers,
 
J

Joerg Jooss

Thus wrote Joerg,
The reason you can easily access Request, Response and other intrinsic
objects is that these are properties of the Page class. If you want to
use them in a library, you'll have to pass them as method parameters.

I shouldn't have said "have to" -- of course one can use HttpContext.Current
to grab the intrinsics, but I usually prefer to use rather explicit interfaces
and not rely on magic (i.e. runtime or host dependent) static members.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top