ThemeAppearance IThemeResolver.GetRequestTheme(RequestContext controllerContext)
        {
            string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey");
            TopicEntity topic = new TopicService().Get(spaceKey);
            if (topic == null)
                throw new ExceptionFacade("找不到专题");

            string themeKey = null;
            string appearanceKey = null;
            PresentArea pa = new PresentAreaService().Get(PresentAreaKeysOfExtension.TopicSpace);
            if (pa == null)
                throw new ExceptionFacade("找不到专题呈现区域");

            if (pa.EnableThemes)
            {
                if (topic.IsUseCustomStyle)
                {
                    themeKey = "Default";
                    appearanceKey = "Default";
                }
                else
                {
                    string[] themeAppearanceArray = topic.ThemeAppearance.Split(',');
                    var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfExtension.TopicSpace, topic.ThemeAppearance);

                    if (appearance != null && themeAppearanceArray.Count() == 2)
                    {
                        themeKey = themeAppearanceArray[0];
                        appearanceKey = themeAppearanceArray[1];
                    }
                }
            }
            if (themeKey == null || appearanceKey == null)
            {
                themeKey = pa.DefaultThemeKey;
                appearanceKey = pa.DefaultAppearanceKey;
            }

            return new ThemeService().GetThemeAppearance(PresentAreaKeysOfExtension.TopicSpace, themeKey, appearanceKey);
        }
        void IThemeResolver.IncludeStyle(RequestContext controllerContext)
        {
            string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey");
            TopicEntity topic = new TopicService().Get(spaceKey);
            if (topic == null)
                return;
            PresentArea presentArea = new PresentAreaService().Get(PresentAreaKeysOfExtension.TopicSpace);
            if (presentArea == null)
                return;


            string themeKey = null;
            string appearanceKey = null;

            IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest<IPageResourceManager>();
            if (!presentArea.EnableThemes)
            {
                string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey);
                string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey, presentArea.DefaultAppearanceKey);

                resourceManager.IncludeStyle(themeCssPath);
                resourceManager.IncludeStyle(appearanceCssPath);
                return;
            }

            if (topic.IsUseCustomStyle)
            {
                var customStyleEntity = new CustomStyleService().Get(presentArea.PresentAreaKey, topic.TopicId);
                if (customStyleEntity == null)
                    return;
                CustomStyle customStyle = customStyleEntity.CustomStyle;
                if (customStyle == null)
                    return;
                string themeCssPath = string.Format("{0}/Custom/theme{1}.css", presentArea.ThemeLocation, customStyle.IsDark ? "-deep" : "");
                string appearanceCssPath = SiteUrls.Instance().CustomStyle(presentArea.PresentAreaKey, topic.TopicId);
                resourceManager.IncludeStyle(themeCssPath);
                resourceManager.IncludeStyle(appearanceCssPath);
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(".tn-page-bg{");
                if (customStyle.IsUseBackgroundImage && !string.IsNullOrEmpty(customStyle.BackgroundImageStyle.Url))
                {
                    builder.AppendLine("background-image:url('" + customStyle.BackgroundImageStyle.Url + @"');");
                    builder.AppendFormat("background-repeat:{0};\n", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat");
                    builder.AppendFormat("background-attachment:{0};\n", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll");
                    string position = "center";
                    switch (customStyle.BackgroundImageStyle.BackgroundPosition)
                    {
                        case BackgroundPosition.Left:
                            position = "left";
                            break;
                        case BackgroundPosition.Center:
                            position = "center";
                            break;
                        case BackgroundPosition.Right:
                            position = "right";
                            break;
                        default:
                            position = "center";
                            break;
                    }
                    builder.AppendFormat("background-position:{0} top;\n", position);
                }

                builder.AppendLine("}");

                resourceManager.RegisterStyleBlock(builder.ToString());
            }
            else
            {
                string[] themeAppearanceArray = topic.ThemeAppearance.Split(',');
                var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfExtension.TopicSpace, topic.ThemeAppearance);

                if (appearance != null && themeAppearanceArray.Count() == 2)
                {
                    themeKey = themeAppearanceArray[0];
                    appearanceKey = themeAppearanceArray[1];
                }
                else
                {
                    themeKey = presentArea.DefaultThemeKey;
                    appearanceKey = presentArea.DefaultAppearanceKey;
                }

                string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeKey);
                string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeKey, appearanceKey);

                resourceManager.IncludeStyle(themeCssPath);
                resourceManager.IncludeStyle(appearanceCssPath);
            }


        }
        /// <summary>
        /// 获取请求页面使用的皮肤
        /// </summary>
        /// <param name="controllerContext"><see cref="RequestContext"/></param>
        ThemeAppearance IThemeResolver.GetRequestTheme(RequestContext controllerContext)
        {
            string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey");
            if (String.IsNullOrEmpty(spaceKey))
                spaceKey = UserContext.CurrentUser.UserName;
            IUserService userService = DIContainer.Resolve<IUserService>();
            User user = userService.GetFullUser(spaceKey);
            if (user == null)
                throw new ExceptionFacade(new ResourceExceptionDescriptor().WithUserNotFound(spaceKey, 0));

            string themeKey = null;
            string appearanceKey = null;
            PresentArea pa = new PresentAreaService().Get(PresentAreaKeysOfBuiltIn.UserSpace);
            if (pa == null)
                throw new ExceptionFacade("找不到用户空间呈现区域");

            if (pa.EnableThemes)
            {
                if (user.IsUseCustomStyle)
                {
                    themeKey = "Default";
                    appearanceKey = "Default";
                }
                else
                {
                    string[] themeAppearanceArray = user.ThemeAppearance.Split(',');
                    var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfBuiltIn.UserSpace, user.ThemeAppearance);

                    if (appearance != null && themeAppearanceArray.Count() == 2)
                    {
                        themeKey = themeAppearanceArray[0];
                        appearanceKey = themeAppearanceArray[1];
                    }
                }
            }

            if (themeKey == null || appearanceKey == null)
            {
                themeKey = pa.DefaultThemeKey;
                appearanceKey = pa.DefaultAppearanceKey;
            }

            return new ThemeService().GetThemeAppearance(PresentAreaKeysOfBuiltIn.UserSpace, themeKey, appearanceKey);
        }
        /// <summary>
        /// 加载皮肤的css
        /// </summary>
        /// <param name="controllerContext"><see cref="RequestContext"/></param>
        void IThemeResolver.IncludeStyle(RequestContext controllerContext)
        {
            string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey");
            if(String.IsNullOrEmpty(spaceKey))
                spaceKey = UserContext.CurrentUser.UserName;
            IUserService userService = DIContainer.Resolve<IUserService>();
            User user = userService.GetFullUser(spaceKey);
            if (user == null)
                throw new ExceptionFacade(new ResourceExceptionDescriptor().WithUserNotFound(spaceKey, 0));

            PresentArea presentArea = new PresentAreaService().Get(PresentAreaKeysOfBuiltIn.UserSpace);
            if (presentArea == null)
                return;

            string themeKey = null;
            string appearanceKey = null;

            IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest<IPageResourceManager>();
            if (!presentArea.EnableThemes)
            {
                string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey);
                string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey, presentArea.DefaultAppearanceKey);

                resourceManager.IncludeStyle(themeCssPath);
                resourceManager.IncludeStyle(appearanceCssPath);
                return;
            }

            if (user.IsUseCustomStyle)
            {
                var customStyleEntity = new CustomStyleService().Get(presentArea.PresentAreaKey, user.UserId);
                if (customStyleEntity == null)
                    return;
                CustomStyle customStyle = customStyleEntity.CustomStyle;
                if (customStyle == null)
                    return;
                string themeCssPath = string.Format("{0}/Custom/theme{1}.css", presentArea.ThemeLocation, customStyle.IsDark ? "-deep" : "");
                string appearanceCssPath = SiteUrls.Instance().CustomStyle(presentArea.PresentAreaKey, user.UserId);
                resourceManager.IncludeStyle(themeCssPath);
                resourceManager.IncludeStyle(appearanceCssPath);
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(".tn-page-bg{");
                if (customStyle.IsUseBackgroundImage)
                {
                    builder.AppendLine("background-image:url('" + customStyle.BackgroundImageStyle.Url + @"');");
                    builder.AppendFormat("background-repeat:{0};\n", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat");
                    builder.AppendFormat("background-attachment:{0};\n", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll");
                    string position = "center";
                    switch (customStyle.BackgroundImageStyle.BackgroundPosition)
                    {
                        case BackgroundPosition.Left:
                            position = "left";
                            break;
                        case BackgroundPosition.Center:
                            position = "center";
                            break;
                        case BackgroundPosition.Right:
                            position = "right";
                            break;
                        default:
                            position = "center";
                            break;
                    }
                    builder.AppendFormat("background-position:{0} top;\n", position);
                }

                builder.AppendLine("}");
                builder.AppendLine("#tn-content{");
                builder.AppendLine("margin-top:" + customStyle.HeaderHeight.ToString() + "px;");
                builder.AppendLine("}");
                resourceManager.RegisterStyleBlock(builder.ToString());
            }
            else
            {
                string[] themeAppearanceArray = user.ThemeAppearance.Split(',');
                var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfBuiltIn.UserSpace, user.ThemeAppearance);

                if (appearance != null && themeAppearanceArray.Count() == 2)
                {
                    themeKey = themeAppearanceArray[0];
                    appearanceKey = themeAppearanceArray[1];
                }
                else
                {
                    themeKey = presentArea.DefaultThemeKey;
                    appearanceKey = presentArea.DefaultAppearanceKey;
                }

                string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeKey);
                string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeKey, appearanceKey);

                resourceManager.IncludeStyle(themeCssPath);
                resourceManager.IncludeStyle(appearanceCssPath);
            }
        }