michielpost.nl

Simple Razor Script Manager

03-08-2011

I needed a simple Razor ScriptManager where I can register the scripts I need, for example a datetimepicker script. But I only want to register the scripts when I'm actually showing the datetime picker.

The datetimepicker is in a PartialView. Inside the partial view, I can now use this HTML helper:
@Script.AddJavaScript("~/Scripts/datetimepicker.js")

On my MasterPage, I can output the registered scripts with this helper:
@Script.OutputJavaScript()

This way the datetimepicker.js script is only included on the page when the PartialView is used. When the same script is registered more than once, it will only output it one time.

There is also support for CSS and you can give an optional CDN location for each registered script. Using the web.config setting UseCDNScripts the use of local or CDN scripts can be toggled.

The Simple Razor Script Manager is available on NuGet.

Comments


No comments yet.

New Comment

Name
E-mail (not published)
Comment
Enter the code shown: