Sometimes you might need to access the Request.Cookies or other properties of the Request object in the Class library (asp.net) under App_Code folder. Sometimes even if your code is compiling properly, you will get error at the runtime.
eg. following code in the class library (inherited with System.Web.UI.MasterPage) compiles properly but throws error null reference exception in the run time.
if (Request.Cookies["CodeF"] != null)
{
string background = Request.Cookies["CodeF"]["BackImage"].ToString();
this.Page.Header.Controls.Add(new LiteralControl(background));
}
The solution if (HttpContext.Current.Request.Cookies["CodeF"] != null)
{
string background = HttpContext.Current.Request.Cookies["CodeF"]["BackImage"].ToString();
this.Page.Header.Controls.Add(new LiteralControl(background));
}
Use the HttpContext.Current.Request object and it will work as expected.
Thanks
Regards,
Sheo Narayan
http://www.dotnetfunda.com