Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get current Page in class file 1

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
hello
I have an aspx page that calls a class in a .cs file.

I need to read a cookie in the cs file. Since the cookie object lies in the System.Web.UI.Page.Request namespace, I need to pass in my current page's object to my class file

example
Code:
[green]// in aspx[/green]

object cookieValue = Common.WebCore.ReadCookie("cookieName", this.Page.Request);
 
[green]// in cs[/green]
static internal string ReadCooke(string CookieName, HttpRequst CurrentRequest)
{
return CurrentRequest.Cookie[CookieName].Value;
}

Is there a way in cs file to not have to request the HttpRequest each time and just read the stack some how?


Thanks,
RalphTrent
 
Is there a way in cs file to not have to request the HttpRequest each time and just read the stack some how?
I don't understand what your asking. This code looks clean to me.

if you mean to want to get the cookie value from the current request without passing the request in you could do this.
Code:
static internal string ReadCooke(string CookieName)
{
return HttpContext.Current.Request.Cookie[CookieName].Value;
}
but i would favor your current implementation over this. I try to avoid as many static references as I can.

here is another option. ore code, fewer static references. in fact there are only 2 and they reside in the same object. the static accessor is just a wrapper around the actual logic.
Code:
class CookieValueExtractor
{
   private readonly HttpRequest request;
   public CookieValueExtractor(HttpRequest request)
   {
      this.request = request;
   }
 
   public T Extract<T>(string key)
   {
       return (T)Convert.ChangeType(request.Cookie[key], typeof(T));
   }
}

//register this in the web.config.
class CookieValueExtractorModule : IHttpModule
{
   private HttpContext context;
   public void Init(HttpApplication context)
   {
      this.context = context.Context;
      context.BeginRequest += InializeCookieValueExtractor; (sender, e) => CurrentSessionContext.Bind(factory.OpenSession());
   }

   private void InializeCookieValueExtractor(object sender, EventArgs e)
   {
       context.Items["CookieValueExtractor"] = new CookieValueExtractor(context.Request);
   }
}
public class CookieValueExtractorAccessor
{
   public static CookieValueExtractor Instance
   {
       get 
       {
            return (CookieValueExtractor)HttpContext.Current.Items["CookieValueExtractor"];
       }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
This is exaclty what I needed

Code:
static internal string ReadCooke(string CookieName)
{
return HttpContext.Current.Request.Cookie[CookieName].Value;
}

Thanks
 
So heres the weird thing. I can create the cookie in Javascript, but when I go to read it in asp.net, its not there. Meaning, I have code that reads from HttpContext.Current.Request.Cookies. In this code, if I am looking for the cookie that sets the clients UTCOffSet, I will create it using the following code :

Code:
System.Web.UI.Page currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
string lstrGetClientUTCOffSet = "<script language='javascript' src='../javascript/onyxreports_core.js'></script><script language='javascript'>setClientsTimeZone();</script>";
		
currentPage.RegisterClientScriptBlock("getClientUTCOffSet",lstrGetClientUTCOffSet);
currentPage = null;

This code works fine, but I then go and read the cookie I just created
Code:
HttpRequest CurrentRequest = HttpContext.Current.Request;
sCookieValue = CurrentRequest.Cookies[CookieName].Value;

I always return null on the first time I execute this code, even though I just created it above, asp.net can not read the javascript cookie until I postback.

Im sure this is a foo-cocked way of doing what I really need to do, (get my clients timezone/utc offset). I am not tied to this code, if there is a better way, I'M all ears.

Thanks,
RalphTrent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top