在Umbraco项目中,获取当前的UmbracoContext几乎是都需要用到的,我们一般通过一个静态方法来获取,代码如下
public class ContextHelpers { public static UmbracoContext EnsureUmbracoContext() { if(UmbracoContext != null) { return UmbracoContext.Current; } var dummyHttpContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("blah.aspx", "", new StringWriter()))); return UmbracoContext.EnsureContext( dummyHttpContext, ApplicationContext.Current, new WebSecurity(dummyHttpContext, ApplicationContext.Current), UmbracoConfig.For.UmbracoSettings(), UrlProviderResolver.Current.Providers, false); } }