Im currently using a method that looks like the following code to add script and css files to the head of the Layout file.
public static class HtmlHelperExtensions
{
public static MyCompanyHtmlHelpers MyCompany(this HtmlHelper htmlHelper)
{
return MyCompanyHtmlHelpers.GetInstance(htmlHelper);
}
}
public class MyCompanyHtmlHelpers
{
private static MyCompanyHtmlHelpers _instance;
public static MyCompanyHtmlHelpers GetInstance(HtmlHelper htmlHelper)
{
if (_instance == null)
_instance = new MyCompanyHtmlHelpers();
_instance.SetHtmlHelper(htmlHelper);
return _instance;
}
private HtmlHelper _htmlHelper;
public ItemRegistrar Styles { get; private set; }
public ItemRegistrar Scripts { get; private set; }
public MyCompanyHtmlHelpers()
{
Styles = new ItemRegistrar(ItemRegistrarFromatters.StyleFormat);
Scripts = new ItemRegistrar(ItemRegistrarFromatters.ScriptFormat);
}
private void SetHtmlHelper(HtmlHelper htmlHelper)
{
_htmlHelper = htmlHelper;
}
}
public class ItemRegistrar
{
private readonly string _format;
private readonly List<string> _items;
public ItemRegistrar(string format)
{
_format = format;
_items = new List<string>();
}
public ItemRegistrar Add(string url)
{
if (!_items.Contains(url))
_items.Insert(0, url);
return this;
}
public IHtmlString Render()
{
var sb = new StringBuilder();
foreach (var item in _items)
{
var fmt = string.Format(_format, item);
sb.AppendLine(fmt);
}
return new HtmlString(sb.ToString());
}
}
public class ItemRegistrarFromatters
{
public const string StyleFormat = "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />";
public const string ScriptFormat = "<script src=\"{0}\" type=\"text/javascript\"></script>";
}
Using Html.MyCompany().Styles.Add("/Dashboard/Content/Dashboard.css");
to add files...
And @Html.MyCompany().Styles.Render()
to render them in Layout_.cshtml.
My problem is that this is a static method meaning it persists the list of stylesheets and script files.
I need to do the same thing that this does but without keeping it persistant.
I need the lists to be remade on every request since they change from page to page what the look are on that specific page.
Is it possible to clear the lists on every request prior to adding the scripts that are needed or maybe after they have been rendered out?
Update: The reason for not using a section, RenderPartial or RenderaActions is to prevent the same stylesheet or script file to be added more than once to the Layout file.
The site im building has a Layout_.cshtml with the basic layout. This in turn is used by a View that loops through a list of items and for each item a RenderAction is called that outputs the specific partial view for that item. These partial views sometimes need to add stylesheets and scripts.
As there can be needed to add many different scripts and stylesheets from different partial views a global list for styles and scripts were the only way i thought this could be done so there is a global place to check if a script is allready added to the collection or not and then render them all at once in the order they were added.
Update 2: The real question is how to do the same kind of function (a global list) but without using a static Extension method.