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);
        }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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()
    {
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x