Anyone using the new Bundling Optimization from .NET 4.5?
I have been reading articles on using the new bundling features with .NET 4.5 and am wondering what type of workflow Sitefinity developers might be using with this, if at all.
I have read numerous articles for both MVC and webform usage, but I am still not sure what the best way to set this up for Sitefinity might be.
Do I just use NuGet and install the bundle package to my Sitefinity site? Then I assume I would setup a global.asax file and create the Bundle.config file?
How well does this all work with css in the themes folders under App_Data?
<
asp:PlaceHolder
runat
=
"server"
>
<%: Scripts.Render("~/bundles/modernizr") %>
</
asp:PlaceHolder
>
<
webopt:BundleReference
runat
=
"server"
Path
=
"~/Content/css"
/>
Hi Stacey,
That's a good one. I normally don't use the structure that Sitefinity describes in their documentation. What I do is:
- Store the masterpages inside ~/App_Master
- Store css, images and scripts in ~/Assets/Css, ~/Assets/Scripts etc.
- Use the Bundling Optimization framework to render the files from the masterpages, like this:
protected
void
Page_Load(
object
sender, EventArgs e)
// Add css files
AddCssReference(
"~/Assets/Styles/css"
);
// Add javascript files
AddJsScriptReferenceBeforeBodyEndTag(
"~/Assets/Scripts/js"
);
// Set the homepage link
lnkHome.NavigateUrl = Helper.GetHomePageUrl(Thread.CurrentThread.CurrentCulture,
true
);
/// <summary>
/// Add javascript references to the header
/// </summary>
/// <param name="relativeScriptPath"></param>
protected
void
AddJsScriptReferenceToHead(
string
relativeScriptPath)
if
(SystemManager.IsDesignMode)
return
;
var scripts = Scripts.Render(
"~/Assets/Scripts/js"
).ToHtmlString();
Page.Header.Controls.Add(
new
Literal() Text = scripts );
/// <summary>
/// Add css references to the header
/// </summary>
/// <param name="relativeCssPath"></param>
protected
void
AddCssReference(
string
relativeCssPath)
var styles = Styles.Render(
"~/Assets/Styles/css"
).ToHtmlString();
Page.Header.Controls.Add(
new
Literal() Text = styles );
Hi Stacey,
Did you make any progess with this? I've just tried, firstly I get this error:
[ArgumentException: File "/bundles/plugins" does not exist.
Parameter name: virtualPath]
Telerik.Sitefinity.Abstractions.VirtualPath.VirtualPathManager.GetCacheDependency(String virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) +449
Any one else who is interested in using Bundling in Sitefinity you need to include an extra line to stop Sitefinity interfering:
protected void Application_Start(object sender, EventArgs e)
BundleConfig.RegisterBundles(BundleTable.Bundles); BundleTable.VirtualPathProvider = System.Web.Hosting.HostingEnvironment.VirtualPathProvider;
BundleTable.EnableOptimizations = true;
Thanks to Yavor for his help.
Dear Daniel, I am missing the code in your post for the method to add script reference before end of body. Would it be possible for you to reveal the code here?
Rgds/Gunnar
Hi Gunnar,
I'm not able to find the code right now. It should be as simple as adding the literal control to the Page.Controls collection:
Page.Controls.Add(
new
Literal() Text = scripts );
Let me know if that works for you?
Best regards,
Daniel
Nope. That does not work.
Server Error in '/' Application.
The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases
.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
Source Error:
Line 77: var scripts = Scripts.Render(relativeScriptPath).ToHtmlString();
Line 78: Line 79: this.Page.Controls.Add(new Literal() Text = scripts ); Line 80:
Line 81:
Hi Gunnar,
OK, probably you need to add this to the Page.Form.Controls collection. Maybe you can try that?
Maybe something like this:
Page.Form.Controls.AddAt(Page.Form.Controls.Count,
new
Literal() Text = scripts1 );
Best,
Daniel