MVC in an external DLL - No definition for StyleSheet when using HtmlHelper
I have created a library assembly targeting .NET Framework 4.6.1.
Within this assembly, I have the standard MVC sub-folder structure and a simple controller, a single model and view. All works nicely... until I started to introduce a custom CSS.
I have the following code within my cshtml file:
@using Telerik.Sitefinity.Frontend;
@using Telerik.Sitefinity.UI.MVC;
<H1>Test</H1>
@Html.StyleSheet(Url.EmbeddedResource("Mine.TestWidget.Resources.Reference", "Mine.TestWidget.Content.MyStyle.css", "bottom"))
But that results in the following exception which I've extracted from the errors log:
Type : System.Web.HttpCompileException, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Message : c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\f6db3324\ce84c46e\App_Web_test.cshtml.692f5287.wodn0rpw.0.cs(606): error CS1061: 'System.Web.Mvc.HtmlHelper<dynamic>' does not contain a definition for 'StyleSheet' and no extension method 'StyleSheet' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<dynamic>' could be found (are you missing a using directive or an assembly reference?)
Source : System.Web
Help link :
Results : System.CodeDom.Compiler.CompilerResults
SourceCode :
WebEventCode : 0
ErrorCode : -2147467259
Data : System.Collections.ListDictionaryInternal
TargetSite : System.CodeDom.Compiler.CompilerResults Compile()
HResult : -2147467259
Stack Trace : at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at Telerik.Sitefinity.Mvc.ControllerWrapper.Execute()
at Telerik.Sitefinity.Mvc.ControllerActionInvoker.ExecuteController(MvcProxyBase proxyControl)
at Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Routing.FeatherActionInvoker.ExecuteController(MvcProxyBase proxyControl)
Any help is appreciated as I'm stuck and run out of ideas!
OK I have made progress and eliminated this error. The following code overcomes the exception:
@using Telerik.Sitefinity.Frontend.Mvc.Helpers;
@using Telerik.Sitefinity.UI.MVC;
<H1>Test</H1>
<button onclick="test123()">Click me</button>
@Html.StyleSheet(Url.EmbeddedResource("Mine.TestWidget.Resources.Reference", "Mine.TestWidget.Content.MyStyle.css"))
@Html.Script(Url.EmbeddedResource(""Mine.TestWidget.Resources.Reference", "Mine.TestWidget.Scripts.MyStyle.js"))
Notice the different @using reference and a change to the Stylesheet method.
I've added my Javascript to this test and I notice isn't not getting rendered in my HTML output. Anyone have tips on how to debug this? The MyStyle.js script has the following code in it:
$(document).ready(function ()
displayAlert("Page ready");
);
function test123()
displayAlert("Testing...");
;
function displayAlert(message)
alert(message);
;
Nothing apprears on page load or when the button is clicked.
This issue is sorted also. The js file wasn't an embedded resource. It was initially but I guess in an attempt to check all code and configuration combinations I set it incorrectly.