Search code examples
c#asp.netasp.net-mvc-3webformsweb-parts

OnActionExecuting equivalent in standard asp.NET?


Is there an equivalent for MVC.NET's OnActionExecuting in standard asp.NET? ?

I thought it would be Page_Load since OnActionExecuting would be called each time an action is executed (or the page loads). But I'm running into inheritance issues when I try to use Page_Load instead.

Since it is very difficult to make my solution work with a Page_Load I'm thinking I might not have the best ... solution.

Any thoughts on whether they are equivalent or close enough?

Background:

I'm converting a piece of an MVC3 application into a standard .NET to wrap in a SharePoint Web Part.

Here's the MVC code I'm trying to translate, as you can see its the user security bits I'm translating:

protected override void OnActionExecuting(ActionExecutingContext filterContext) {

            if (!SiteCacheProvider.ItemCached(enmCacheKey.SiteSetting)) {

                if (filterContext.IsImplementedGeneralPrincipal()) {
                    IUserProfile userProfile = ((IGeneralPrincipal)filterContext.HttpContext.User).UserProfile;

                    SiteCacheProvider.ChangeSiteSetting(userProfile.SiteID);
                }
            }

            base.OnActionExecuting(filterContext);
        }

Solution

  • First, take on account that no Actions are in ASP.NET because the model is different (Event-Based) - There're no methods(actions) which you can decorate with Action Filters, it's all about the Page-Cycle events.

    Second, In ASP.NET, you may use HTTP modules (HttpApplication.BeginRequest particularly) in order to intercept incoming requests to your application pages by adding your required logic.

    From MSDN:

    HTTP Modules use to intercept HTTP requests for modifying or utilize HTTP based requests according to needs like authentication, authorization, session/state management, logging, modifying Response, URL rewriting, Error handling, Caching....

    For example:

    using System;
    using System.Web;
    using System.Collections;
    
    public class HelloWorldModule : IHttpModule
    {
        public string ModuleName
        {
            get { return "HelloWorldModule"; }
        }
    
        public void Init(HttpApplication application)
        {
             application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
             application.EndRequest += (new EventHandler(this.Application_EndRequest));
    
        }
    
        private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Write("<h1>HelloWorldModule: Beginning of Request</h1><hr>");
        }
        private void Application_EndRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Write("<hr><h1>HelloWorldModule: End of Request</h1>");
        }
        public void Dispose()
        {
        }
    }