Click here to Skip to main content
15,881,757 members
Articles / Web Development / IIS
Article

Migrating ASP.NET Applications to IIS 7 Integrated Mode

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 14.1K   1   1
Introduction There had been much discussion on how to make your application compatible with the IIS7 Integrated mode. To know how to make your

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Introduction

There had been much discussion on how to make your application compatible with the IIS7 Integrated mode. To know how to make your application compatible with IIS7 integrated mode, you first need to understand what the integrated mode is and why we want to use it.

IIS7 released with two modes, Classic mode and the Integrated mode. The former is compatible with previous versions of the IIS. While the later is the enhanced one which supports many features.

In this blog I am going to explain about the “Request is not available in this context” exception.

The “Request is not available in this context” exception is one of the more common errors you may receive when moving ASP.NET applications to Integrated mode on IIS 7.0/7.5.  This exception happens in your implementation of the Application_Start method in the global.asax file when you attempt to access the HttpContext of the request.  It looks something like this:

Server Error in '/' Application.


Request is not available in this context

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Request is not available in this context

Source Error:

 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

 


As
ASP.NET applications were always started by the first request to the application; it used to be possible to get to the request context through the static HttpContext.Current field. In the Classic mode the request context is used to available.

But because of the design change in the IIS7 Integrated Pipeline, makes the request context unavailable in the Application_Start event.

So what if you want to access the request context in the Application_Start event,

  1. Either you can move the application to the Classic mode which is not recommended, Or
  2. You can change your application accordingly.

If your choice is to change your application, then you first need to remove the references to HttpContext.Current from the Application_Start.

Firstly if you are using the HttpContext.Current.Request for getting the application path you don’t need to user the request, use HttpRuntime.AppDomainAppVirtualPath instead.

See the code below;

Response.Write(HttpRuntime.AppDomainAppVirtualPath);
Response.Write(HttpContext.Current.Request.ApplicationPath);

Both will return the same thing.

 

Or if you want to access the first Request information, here is the sample code if you want to access the request information in Application_Start event:

void Application_BeginRequest(object sender, EventArgs e) 
{
    HttpApplication application = (HttpApplication)sender;
    HttpContext context = application.Context;
    // Initialise your first request here..
}

Get the context and then do you first initialization steps.

On next blog we will be discovering more about "Migrating ASP.NET Applications to IIS 7 Integrated Mode".

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
BugRequest is not available in this context exception in Application_Start Pin
Arulraja Sellaperumal28-Sep-15 2:56
Arulraja Sellaperumal28-Sep-15 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.