【问题标题】:Umbraco Application BeginRequest never firedUmbraco 应用程序 BeginRequest 从未被触发
【发布时间】:2015-07-02 16:25: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 函数中记录消息。

【问题讨论】:

    标签: c# asp.net umbraco umbraco7


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      据此,您应该在 v6.1.0 及更高版本中实现 ApplicationEventHandler,而不是 IApplicationEventHandler:https://our.umbraco.org/documentation/Reference/Events/application-startup

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-03
          • 2012-07-07
          • 2018-04-13
          相关资源
          最近更新 更多