Umbraco中获取UmbracoContext

在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);


    }


}

 

posted on 2018-11-08 05:47  新西兰程序员  阅读(165)  评论(0编辑  收藏  举报