我正在使用 umbraco 7.0,我需要在应用程序启动、 session 开始和 session 结束时添加一些自定义代码。至于在umbraco中注册事件,我们必须继承Umbraco.Core.ApplicationEventHandler
,我有这样的。但我们只能覆盖 ApplicationStarted
而不能像 Global.asax 中那样覆盖 Session 相关的内容。
我见过Global.asax in Umbraco 6但我无法访问 Session,如该答案 if (Session != null && Session.IsNewSession)
所示,也许是 umbraco 6 并且在 umbraco 7 中发生了一些变化。
有什么解决办法吗?
这是上述帖子中建议的代码。
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
}
}
最佳答案
您的方向是正确的,您只需找到 Session
来自 sender
的对象作为 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
}
}
}
关于asp.net-mvc - Umbraco 中的 session 开始和 session 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765425/