During the development of Claymore
, for the most part ASP.NET Specifically, I needed to find a hookgiving me the opportunity to interact with the pages before the events found on the Page object.
I first thought of tinkering in the Global.asax file ... without having finally found what I wanted. By cons, digging a bit on the side of
HttpModule
I arrived at the following solution:using System; using System.Reflection; using System.Web; using System.Web.UI;
namespace Claymore.Web
{
/// <summary>
///
/// </summary>
public class ClaymoreHttpModule : IHttpModule
{
#region Fields
private HttpApplication _application;
#endregion
/// <summary>
/// Initializes a module and prepares it to handle requests.
/// </summary>
/// <param name="context"> </param>An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application
public void Init(HttpApplication context)
{
if(context == null)
throw new ArgumentNullException("context");
_application = context;
context.PostMapRequestHandler += onPostMapRequestHandler;
}
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
/// </summary>
public void Dispose(){}
/// <summary>
/// Handle PostMapRequest event.
/// </summary>
/// <param name="sender"> </param>The sender.
/// <param name="e"> </param>The <see cref="System.EventArgs"/> instance containing the event data.
private void onPostMapRequestHandler(object sender, EventArgs e)
{
Page pageHandler;
if ((pageHandler = _application.Context.Handler as Page) != null)
pageHandler.PreInit += HandlePreInit;
}
/// <summary>
/// Handles The Pre Init event. / / / \u0026lt;/ Summary> / / / \u0026lt;param name="sender"> \u0026lt;/ param> The sender. / / / \u0026lt;param Name="e"> \u0026lt;/ param> The body containing \u0026lt;see cref="System.EventArgs"/> The Event data. protected virtual void HandlePreInit (object sender, EventArgs e) {Page page = sender as Page; if (page! = null) {/ / And voila, you have a pointer to a page, and you can interact with it before 'Init event. / / Add code here. } }}}
0 comments:
Post a Comment