Why HttpContext?

J

Jason Madison

I have been reading some asp.net code and wondered why it uses HttpContext.

On one page this is done:

// ------------------------------------------------------------------------
HttpContext ctx = HttpContext.Current;
string chartID = Guid.NewGuid ().ToString ();

ctx.Session [chartID] = byteArr;
imgHondaLineup.ImageUrl = string.Concat ("chart.ashx?", chartID);

then in the code behind the chart.ashx page this is done:

public void ProcessRequest (HttpContext ctx)
{
string chartID = ctx.Request.QueryString[0];
Array arr = (Array) ctx.Session [chartID];
....
ctx.Response.ContentType = "image/png";
ctx.Response.StatusCode = 200;
ctx.Response.End ();

What I want to understand is what would be the difference if the HttpContext
instance wasn't used. E.g.

Session [chartID] = byteArr;

....
string chartID = Request.QueryString[0];
Array arr = (Array) Session [chartID];
....
....
Response.ContentType = "image/png";
Response.StatusCode = 200;

etc.

Thanks
 
B

Brock Allen

These APIs like Request, Response, Session, etc, are made available to the
current scope typically as a property you've inherited from a base class
(like Page, or Control). In some contexts there is no base class that has
made those objects available, so the static HttpContext.Current property
is another way to acces those objects. It makes all the info about the current
HTTP request available.
 

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,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top