gpt4 book ai didi

c# - Umbraco 应用程序 BeginRequest 从未被解雇

转载 作者:行者123 更新时间:2023-11-30 20:42:38 24 4
gpt4 key购买 nike

我想在 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 的方式。首先创建一个继承自 UmbracoApplication 的新类(参见下面的示例),然后更新您的 global.asax 以继承您的新类。

public class MyUmbracoApplication : Umbraco.Web.UmbracoApplication
{
private void Application_BeginRequest(object sender, EventArgs e)
{
/* Your code here */
}
}

关于c# - Umbraco 应用程序 BeginRequest 从未被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31189884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com