using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using Umbraco.Core; using Umbraco.Core.Events; namespace Umbraco.Web { /// /// Provides extension methods for . /// public static class UmbracoContextExtensions { /// /// tries to get the Umbraco context from the HttpContext /// /// /// /// /// This is useful when working on async threads since the UmbracoContext is not copied over explicitly /// public static UmbracoContext GetUmbracoContext(this HttpContext http) { return GetUmbracoContext(new HttpContextWrapper(http)); } /// /// tries to get the Umbraco context from the HttpContext /// /// /// /// /// This is useful when working on async threads since the UmbracoContext is not copied over explicitly /// public static UmbracoContext GetUmbracoContext(this HttpContextBase http) { if (http == null) throw new ArgumentNullException("http"); if (http.Items.Contains(UmbracoContext.HttpContextItemName)) { var umbCtx = http.Items[UmbracoContext.HttpContextItemName] as UmbracoContext; return umbCtx; } return null; } /// /// If there are event messages in the current request this will return them , otherwise it will return null /// /// /// public static EventMessages GetCurrentEventMessages(this UmbracoContext umbracoContext) { var eventMessagesFactory = umbracoContext.Application.Services.EventMessagesFactory as ScopeLifespanMessagesFactory; return eventMessagesFactory == null ? null : eventMessagesFactory.TryGet(); } } }