Accessing page request / server variables in a class file.

  • Thread starter Thread starter jgamble
  • Start date Start date
J

jgamble

Hi,

Here's a question for you all. Under "classic" ASP any ASP page could
have any number of #Include files. Using #Include files I could have
functions which were common to my whole site and these files had
access to the Request, Session and Server objects from the page they
were included on.

How can this be done under .NET? My understanding is that these
functions that were once in #Include files should really be part of
some compiled class which is instantiated on the code-behind page. So
then my question really is, how can the compiled class reference the
Request and Server objects of the page that instantiated the class?

Here's some pseudo code for illustrative purposes....

File = default.aspx.cs

public class _default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
try
{
// Instantiate my class ...
classes.c_env C = new classes.c_env();
Response.Write(C.pageName);
} catch (Exception e) {
Response.Write(e.Message);
}
}



File = /Classes/c_env.cs

public class c_env
{
private string pagename;

// Constructor
public c_env()
{
this.pagename = "";
}

// Properties
public string pageName
{
get
{
return Request.ServerVariables["SCRIPT_NAME"].ToString();
}
}
}

How can this separate class access
Request.ServerVariables["SCRIPT_NAME"].ToString(); from the
instantiating aspx page?

Thanks

John
 
Import System.Web namespace into the class. Then you can access Context by
using:

HttpContext.Current.Response.Write("Something");
HttpContext.Current.Session["key"]="Value";

and so on.

--
Teemu Keiski
MCP,Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com

Email:
(e-mail address removed)
 
Back
Top