Umbraco应用程序BeginRequest永远不会被解雇

时间:2015-07-02 16:25:48

标签: c# asp.net umbraco umbraco7

我想在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(不是快速),我无法在函数 umbracoApplication_BeginRequest 中记录消息。

3 个答案:

答案 0 :(得分:2)

这是我能够在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 */
    }
}

答案 1 :(得分:1)

根据这个,你应该在v6.1.0和forward中实现ApplicationEventHandler而不是IApplicationEventHandler:https://our.umbraco.org/documentation/Reference/Events/application-startup

答案 2 :(得分:0)

首先,您应该创建一个继承 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>