Beispiel #1
0
        private static MvcHtmlString RegisterResource(HttpContextBase httpContext, string resourcePath, ResourceType resourceType, string sectionName, bool throwException)
        {
            throwException = throwException && httpContext.CurrentHandler != null;

            var registerName = string.Empty;

            if (resourceType == ResourceType.Js)
            {
                registerName = ResourceHelper.JsRegisterName;
            }
            else if (resourceType == ResourceType.Css)
            {
                registerName = ResourceHelper.CssRegisterName;
            }

            var register = new ResourceRegister(registerName, httpContext);

            MvcHtmlString result = MvcHtmlString.Empty;

            // No section name renders the script inline if it hasn't been rendered
            if (string.IsNullOrEmpty(sectionName) || !SectionRenderer.IsAvailable(httpContext.Handler.GetPageHandler(), sectionName))
            {
                if (!register.IsRegistered(resourcePath, sectionName: null))
                {
                    result = MvcHtmlString.Create(ResourceHelper.BuildSingleResourceMarkup(resourcePath, resourceType));
                }
            }

            // Register the resource even if it had to be rendered inline (avoid repetitions).
            register.Register(resourcePath, sectionName, throwException);

            return(result);
        }
Beispiel #2
0
        private static MvcHtmlString RegisterResource(HttpContextBase httpContext, string resourcePath, ResourceType resourceType, string sectionName, bool throwException, List <KeyValuePair <string, string> > attributes = null)
        {
            throwException = throwException && httpContext.CurrentHandler != null;

            var registerName = string.Empty;

            if (resourceType == ResourceType.Js)
            {
                registerName = ResourceHelper.JsRegisterName;
            }
            else if (resourceType == ResourceType.Css)
            {
                registerName = ResourceHelper.CssRegisterName;
            }

            var register = new ResourceRegister(registerName, httpContext);

            MvcHtmlString result = MvcHtmlString.Empty;

            if (!string.IsNullOrWhiteSpace(sectionName))
            {
                var pageHandler = httpContext.Handler.GetPageHandler();
                if (pageHandler != null && pageHandler.Master is MvcMasterPage)
                {
                    if (!throwException && !SectionRenderer.IsAvailable(pageHandler, sectionName))
                    {
                        sectionName = null;
                    }
                }
                else
                {
                    if (!SectionRenderer.IsAvailable(pageHandler, sectionName))
                    {
                        sectionName = null;
                    }
                }
            }
            else
            {
                sectionName = null;
            }

            // No section name renders the script inline if it hasn't been rendered
            if (sectionName == null ||
                ResourceHelper.RenderScriptSection)
            {
                if (!register.IsRegistered(resourcePath))
                {
                    result = MvcHtmlString.Create(ResourceHelper.BuildSingleResourceMarkup(resourcePath, resourceType, sectionName, attributes));
                }
            }

            // Register the resource even if it had to be rendered inline (avoid repetitions).
            register.Register(resourcePath, sectionName, throwException, attributes);

            return(result);
        }
        private static string BuildHtmlResourcesMarkup(ResourceRegister resourceRegister, string sectionName, ResourceType resourceType)
        {
            StringBuilder output = new StringBuilder();

            foreach (var resource in resourceRegister.GetResourcesForSection(sectionName))
            {
                if (!resourceRegister.IsRendered(resource))
                {
                    output.Append(ResourceHelper.BuildSingleResourceMarkup(resource, resourceType, sectionName));
                    resourceRegister.MarkAsRendered(resource);
                }
            }

            return(output.ToString());
        }