Umbraco应用程序BeginRequest从未启动
本文关键字:启动 BeginRequest 应用程序 Umbraco | 更新日期: 2023-09-27 18:00:48
我想在Umbraco中触发BeginRequest事件,但它不起作用。其余的代码运行良好。
public class ApplicationEventHandler : IApplicationEventHandler
{
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { }
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { }
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
umbracoApplication.BeginRequest += umbracoApplication_BeginRequest;
BundleConfig.RegisterBundles(BundleTable.Bundles);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}
void umbracoApplication_BeginRequest(object sender, EventArgs e)
{
// Create HttpApplication and HttpContext objects to access
// request and response properties.
UmbracoApplicationBase application = (UmbracoApplicationBase)sender;
HttpContext context = application.Context;
if (context.Response.Cookies[Const.LANGUAGE_COOKIE_NAME] == null)
{
context.Response.Cookies.Add(new HttpCookie(Const.LANGUAGE_COOKIE_NAME, Thread.CurrentThread.CurrentUICulture.Name));
return;
}
//cookie exists already
else
{
//if no 404
if (UmbracoContext.Current.PublishedContentRequest != null && !UmbracoContext.Current.PublishedContentRequest.Is404)
{
//cookie value different than the current thread: user switched language.
if (context.Response.Cookies[Const.LANGUAGE_COOKIE_NAME].Value != Thread.CurrentThread.CurrentUICulture.Name)
{
//we set the cookie
context.Response.Cookies[Const.LANGUAGE_COOKIE_NAME].Value = Thread.CurrentThread.CurrentUICulture.Name;
}
}
}
}
}
你知道为什么它不起作用吗?我使用的是umbraco 7,本地IIS(非express),无法在函数umbracoApplication_BeginRequest中记录消息。
这就是我在Umbraco 7.1.2实例中附加BeginRequest的方式。首先创建一个从UmbracApplication继承的新类(请参阅下面的示例),然后更新global.asax以从新类继承。
public class MyUmbracoApplication : Umbraco.Web.UmbracoApplication
{
private void Application_BeginRequest(object sender, EventArgs e)
{
/* Your code here */
}
}
根据这一点,您应该在v6.1.0中实现ApplicationEventHandler,而不是IApplicationEventHandler,并转发:https://our.umbraco.org/documentation/Reference/Events/application-startup
首先,您应该创建一个继承System.Web.IHttpModule接口的HttpModel类
public class HttpModule : IHttpModule
{
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += ContextBeginRequest;
}
private void ContextBeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app != null)
{
//do stuff
}
}
void IHttpModule.Dispose()
{
// Nothing to dispose;
}
}
然后,在web.config 中
<system.webServer><modules runAllManagedModulesForAllRequests="true">
<remove name="DoStuff" />
<add name="DoStuff" type="YourNameSpace.HttpModule, YourAssembly" /></modules></system.webServer>