In my Global.asax.cs file I have the following methods:
protected virtual void Application_BeginRequest() {
HttpContext.Current.Items["message"] = "Hello World";
}
protected virtual void Application_EndRequest() {
string message = (string)HttpContext.Current.Items["message"];
if (String.IsNullOrEmpty(message)) {
HttpContext.Current.Response.Write("<!-- Empty Message! -->");
}
else {
HttpContext.Current.Response.Write("<!-- Message: " + message + " -->");
}
}
When I run locally, the message is output after the closing </html>
tag.
When I deploy to the test server the message is not output, not even the <!-- Empty Message! -->
one. I can even add code to throw an exception in the Application_EndRequest
method which never gets thrown.
I have used this approach with previous projects, built with VS2010 and MVC 4, mainly for the one dbcontext per request solution, with no problems.
I discovered this as part of an investigation as to why HttpContext.Current.Items
was losing values during a request, again only when deployed to the test server. (Similar to this unresolved problem)
The application is straight 'out-of-the-box' using Visual Studio 2013 and MVC 5 (i.e. a new MVC application and no custom code), so there have been very few changes to web.config
. In fact the only change was this to get around the empty page problem
<system.webServer>
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
</system.webServer>
Does anyone know why Application_EndRequest
would not be getting called on the server?
Is there yet another configuration setting needed?
Update 201412191534
I finally got Glimpse working. I added some TraceInformation
messages to Application_Start
, Application_BeginRequest
and Application_EndRequest
. Running locally the trace messages appear. Deploy app to test server and none of the messages appear. So it would seem none of the methods on global.asax are getting called on the server, which would explain my missing HttpContext.Current.Items
- they had not been set in the first place!
Update 201412221010
I have now looked into why the events in Global.asax are not getting called and found many similar questions:
- Events in Global.asax are not firing
- Global.asax Events not firing in IIS 6
- Global.asax is not publishing and event are not firing in Global.asax
- Why are the Global.asax events not firing in my ASP .NET website?
I have tried such suggestions as:
- making sure the Global.asax inherits from the correct class (It does);
- setting a code break point (which is never reached because the events aren't fired)
Then I found this one - Global.asax not firing for .aspx pages in IIS7 which suggested using the <modules runAllManagedModulesForAllRequests="true">
which I hadn't because I was using the approach of adding the following to the modules
section
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
because everyone recommends not using runAllManagedModulesForAllRequests="true"
. See this article Don't use runAllManagedModulesForAllRequests="true" when getting your MVC routing to work
However, in a desparate attempt I used this evil attribute and guess what? The events started getting fired again!
We haven't applied the hotfix and I haven't found out how to add modules into the modules
section i.e. the correct syntax for my application.