I got this error strangely enough in one of my ASP.NET web forms projects. I tried debugging through the application, but could never get Visual Studio to hit the actual error. Some research leads to the following two posts that may offer some help:
- IIS Integrated Mode and Application Start
- “Request is not available in this context” on Nested User Control
Both of these fixes are valid; however, in my scenario, the problem was isolated to the page. My page had a constructor, which checked information within the current request. To access the request was done using:
public MyPage()
{
string var = this.Request.QueryString.Get(“x”);
}
This caused the exception to occur. The reason is, even though Page.Request is a valid way to refer to the request, it’s happening too early and needs to be replaced with HttpContext.Current.Request. In certain other scenarios, accessing page.Request happens too early or at the wrong time, and needs to be replaced by HttpContext.Current.Request.