-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHtmlHelperExtensions.cs
More file actions
108 lines (98 loc) · 3.77 KB
/
HtmlHelperExtensions.cs
File metadata and controls
108 lines (98 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
namespace System.Web.Mvc
{
public enum AssetHierarchy
{
Layout = 0,
Page = 1,
Partial = 2
}
public static class HtmlHelperExtensions
{
private class Asset
{
public AssetHierarchy AssetHierarchy { get; set; }
public string Url { get; set; }
}
private static List<Asset> Scripts
{
get
{
if (!HttpContext.Current.Items.Contains("user-request-script-items"))
{
HttpContext.Current.Items["user-request-script-items"] = new List<Asset>();
}
return (HttpContext.Current.Items["user-request-script-items"] as List<Asset>);
}
}
private static List<Asset> Styles
{
get
{
if (!HttpContext.Current.Items.Contains("user-request-style-items"))
{
HttpContext.Current.Items["user-request-style-items"] = new List<Asset>();
}
return (HttpContext.Current.Items["user-request-style-items"] as List<Asset>);
}
}
/// <summary>
/// Renders the scripts onto the page that were added by the view and partial views.
/// </summary>
/// <param name="html">The HTMLHelper</param>
/// <returns></returns>
public static MvcHtmlString RenderScripts(this HtmlHelper html)
{
var sb = new StringBuilder();
foreach (var script in Scripts.OrderBy(i => i.AssetHierarchy))
{
sb.AppendLine(String.Format("<script src=\"{0}\"></script>", UrlHelper.GenerateContentUrl(script.Url, html.ViewContext.HttpContext)));
}
return new MvcHtmlString(sb.ToString());
}
/// <summary>
/// Renders the stylesheets onto the page that were added by the view and partial views.
/// </summary>
/// <param name="html">The HTMLHelper</param>
/// <returns></returns>
public static MvcHtmlString RenderStyleSheets(this HtmlHelper html)
{
var sb = new StringBuilder();
//Styles.Reverse();
foreach (var style in Styles.OrderBy(i => i.AssetHierarchy))
{
sb.AppendLine(String.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\">", UrlHelper.GenerateContentUrl(style.Url, html.ViewContext.HttpContext)));
}
return new MvcHtmlString(sb.ToString());
}
/// <summary>
/// Adds a script to be emitted by the RenderScripts method.
/// </summary>
/// <param name="html">The HTMLHelper.</param>
/// <param name="scriptUrl">The script URL.</param>
/// <param name="scriptUrl">The display priority.</param>
public static void AddScript(this HtmlHelper html, string scriptUrl, AssetHierarchy hierarchy = AssetHierarchy.Page)
{
Scripts.Add(new Asset { Url = scriptUrl , AssetHierarchy = hierarchy});
}
/// <summary>
/// Adds a stylesheet to be emitted by the RenderStyleSheets method.
/// </summary>
/// <param name="html">The HTMLHelper</param>
/// <param name="styleUrl">The stylesheet URL.</param>
/// <param name="scriptUrl">The display priority.</param>
///
public static void AddStyleSheet(this HtmlHelper html, string styleUrl, AssetHierarchy hierarchy = AssetHierarchy.Page)
{
Styles.Add(new Asset { Url = styleUrl, AssetHierarchy = hierarchy });
}
}
}