问题
I am using umbraco 7.0 and I need to add some custom code on application start, session start and session end. As for registering events in umbraco we have to inherit from Umbraco.Core.ApplicationEventHandler
, I have sone so. But in that we can only override ApplicationStarted
and not Session related as we can do in Global.asax.
I have seen Global.asax in Umbraco 6 but I can not access Session as shown in that answer if (Session != null && Session.IsNewSession)
, maybe it was umbraco 6 and something changed in umbraco 7.
Any solutions?
Heres the code suggested in mentioned post.
public class Global : Umbraco.Web.UmbracoApplication
{
public void Init(HttpApplication application)
{
application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
application.EndRequest += (new EventHandler(this.Application_EndRequest));
//application.Error += new EventHandler(Application_Error); // Overriding this below
}
protected override void OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
// Your code here
}
private void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
try
{
if (Session != null && Session.IsNewSession) // Not working for me
{
// Your code here
}
}
catch(Exception ex) { }
}
private void Application_BeginRequest(object sender, EventArgs e)
{
try { UmbracoFunctions.RenderCustomTree(typeof(CustomTree_Manage), "manage"); }
catch { }
}
private void Application_EndRequest(object sender, EventArgs e)
{
// Your code here
}
protected new void Application_Error(object sender, EventArgs e)
{
// Your error handling here
}
}
回答1:
You are on the right track, you just need to find the Session
object from the sender
that is passed to you as the argument in PreRequestHandlerExecute
.
public class Global : UmbracoApplication
{
public override void Init()
{
var application = this as HttpApplication;
application.PreRequestHandlerExecute += PreRequestHandlerExecute;
base.Init();
}
private void PreRequestHandlerExecute(object sender, EventArgs e)
{
var session = ((UmbracoApplication)sender).Context.Session;
if (session != null && session.IsNewSession)
{
// Your code here
}
}
}
来源:https://stackoverflow.com/questions/29765425/session-start-and-session-end-in-umbraco