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); }
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); }
public void RegisterResource_AlreadyRegisteredResource_ExceptionIsThrown() { // Arrange string registerName = "TestRegister"; HttpContextBase context = this.CreateHttpContext(); var register = new ResourceRegister(registerName, context); string fakeResourceKey = "test-resource"; register.Register(fakeResourceKey); Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1); // Act register.Register(fakeResourceKey, throwException: true); // Assert Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1); }
public void TryRegisterResource_AlreadyRegisteredResource_ResourceIsNotRegisteredTwice() { // Arrange string registerName = "TestRegister"; HttpContextBase context = this.CreateHttpContext(); var register = new ResourceRegister(registerName, context); string fakeResourceKey = "test-resource"; register.Register(fakeResourceKey); Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1); // Act bool result = register.Register(fakeResourceKey); // Assert Assert.IsFalse(result); Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1); }
public void RegisterResource_NewResource_ResourceIsRegistered() { // Arrange string registerName = "TestRegister"; HttpContextBase context = this.CreateHttpContext(); var register = new ResourceRegister(registerName, context); string fakeResourceKey = "test-resource"; Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 0); // Act register.Register(fakeResourceKey); // Assert Assert.IsTrue(register.GetInlineResources().Count(i => i == fakeResourceKey) == 1); }