Giu
Giu

Reputation: 1952

Umbraco Application BeginRequest never fired

I want to fire the BeginRequest event in Umbraco but it doesn't work. The rest of the code works just fine.

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;
                }
            }
        }
    } 
}

Do you have any idea why it is not working ? I am using umbraco 7, local IIS (not express) and I can't log messages inside the function umbracoApplication_BeginRequest.

Upvotes: 2

Views: 1763

Answers (3)

Cat Love Bear
Cat Love Bear

Reputation: 11

First, you should create a HttpModel class that inherit System.Web.IHttpModule interface

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; 
    }
}

Then, in your web.config

<system.webServer><modules runAllManagedModulesForAllRequests="true">
  <remove name="DoStuff" />
  <add name="DoStuff" type="YourNameSpace.HttpModule, YourAssembly" /></modules></system.webServer>

Upvotes: 1

Keith
Keith

Reputation: 5391

This is how I was able to attach to BeginRequest in an Umbraco 7.1.2 instance. First create a new class that inherits from UmbracoApplication (see sample below), then update your global.asax to inherit from your new class.

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

Upvotes: 3

Jannik Anker
Jannik Anker

Reputation: 3425

According to this, you should be implementing ApplicationEventHandler, not IApplicationEventHandler, in v6.1.0 and forwards: https://our.umbraco.org/documentation/Reference/Events/application-startup

Upvotes: 1

Related Questions