Page_Init
is the earliest event you can subscribe to in WebForms, so that is the way to go.
sometimes the counter increments by more than 1 (I assume if there's a URL re-write or redirect).
How is that a problem? A redirect is followed by another request, so it is another request. If you want to exclude those, you'll have to check in the latest event (Page_Unload
in the case of WebForms) whether the response will perform a redirect and if so, subtract one from the counter, if that is the desired result.
Alternatively see Best way to implement request throttling in ASP.NET MVC? if you don't want to reinvent the wheel.
As per @mathieu's answer, you can swap Page_Init
with HttpApplication.BeginRequest
and Page_Unload
with HttpApplication.EndRequest
and perform the same logic, to make it independent of WebForms. You can hook up these events in Global.asax.