In my site, because I have a lot of resources, asynchronously loaded panels, compressed scripts and styles etc everytime I debug the BeginRequest method in Global.asax it tends to get hit 5 or 6 times due to all the content requests.
public MvcApplication()
{
BeginRequest += MvcApplication_BeginRequest;
EndRequest += MvcApplication_EndRequest;
AcquireRequestState += MvcApplication_AcquireRequestState;
}
private void MvcApplication_BeginRequest(object sender, EventArgs e)
{
var nHibernateContext = Ioc.ContainerWrapper.Resolve<INHibernateContext>();
CurrentSessionContext.Bind(nHibernateContext.SessionFactory.OpenSession());
MyMethodWithSomeComplexCookieLogic();
}
I would ideally want my MyMethodWithSomeComplexCookieLogic()
to be processed only once with every page load and not 5 or 6 times depending on the page I am going.
Is there a way to filter the main page BeginRequest from all the smaller requests?
Alternatively where else can this logic be moved to make sure that it is handled once on every page load globally?
I have done some reading and I can't find an appropriate solution.
I tried creating a Filter Attribute:
public class MyMethodFilterAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
base.OnResultExecuting(context);
/* my logic */
}
}
And I registered that in Global.asax
private static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyMethodFilterAttribute());
}
But it gets hit 5 or 6 times.
Is MVC unable to support a similar functionality to the Page_Load? In a classic .NET application the events get only hit once.
Thank you