I'm trying unsuccessfully to get script and style bundling working in a .NET framework 4.8 Webforms application with limited success.
I've installed the Microsoft.AspNet.Web.Optimization NuGet package and have built my Bundle Config class according to the documentation, and I call the RegisterRoutes in the Application_Start in global.asax, but when I try to get the bundles to render on the master page, I get errors and the page doesn't load.
in BundlesConfig.cs...
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new StyleBundle("~/bundles/SiteCSS").Include(
"~/Theme/bootstrap/css/bootstrap.css",
"~/Theme/fontawesome/css/all.css",
"~/Theme/site/site.css"
));
bundles.Add(new ScriptBundle("~/bundles/SiteJS")
.Include(
"~/Theme/jquery/js/jquery-3.6.0.js",
"~/Theme/bootstrap/js/bootstrap.js",
"~/Theme/fontawesome/js/all.js",
"~/Theme/site/site.js"
));//.Transforms.Add(new JsMinify());
}
}
in global.asax...
protected void Application_Start(object sender, EventArgs e)
{
BundleTable.EnableOptimizations = true;
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
But when I come to add the following in the site master page...
<asp:PlaceHolder runat="server">
<%: Styles.Render("~/bundles/SiteCss") %>
</asp:PlaceHolder>
and
<asp:PlaceHolder runat="server">
<%: Scripts.Render("~/bundles/SiteJS") %>
</asp:PlaceHolder>
I get the errors...
CS0103: The name 'Styles' does not exist in the current context.
and
CS0103: The name 'Scripts' does not exist in the current context.
It feels like I've done all I need to do as it appears to be fairly straight forward, but its just not working...
Any ideas?
Thanks, Karl