This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
HtmlHelpers.cs
136 lines (118 loc) · 5.16 KB
/
HtmlHelpers.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
namespace Nancy.ViewEngines.Razor
{
using System;
using System.IO;
using Nancy.Extensions;
using Nancy.Security;
/// <summary>
/// Helpers to generate html content.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
public class HtmlHelpers<TModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="HtmlHelpers{T}"/> class.
/// </summary>
/// <param name="engine">The razor view engine instance that the helpers are being used by.</param>
/// <param name="renderContext">The <see cref="IRenderContext"/> that the helper are being used by.</param>
/// <param name="model">The model that is used by the page where the helpers are invoked.</param>
public HtmlHelpers(RazorViewEngine engine, IRenderContext renderContext, TModel model)
{
this.Engine = engine;
this.RenderContext = renderContext;
this.Model = model;
}
/// <summary>
/// The model that is being used by the current view.
/// </summary>
/// <value>An instance of the view model.</value>
public TModel Model { get; set; }
/// <summary>
/// The engine that is currently rendering the view.
/// </summary>
/// <value>A <see cref="RazorViewEngine"/> instance.</value>
public RazorViewEngine Engine { get; set; }
/// <summary>
/// The context of the current render operation.
/// </summary>
/// <value>An <see cref="IRenderContext"/> instance.</value>
public IRenderContext RenderContext { get; set; }
/// <summary>
/// Renders a partial with the given view name.
/// </summary>
/// <param name="viewName">Name of the view.</param>
/// <returns>An <see cref="IHtmlString"/> representation of the partial.</returns>
public IHtmlString Partial(string viewName)
{
if (string.IsNullOrWhiteSpace(viewName)) throw new ArgumentNullException("viewName", "Attempted to render a non-existent view");
return this.Partial(viewName, null);
}
/// <summary>
/// Renders a partial with the given view name.
/// </summary>
/// <param name="viewName">Name of the partial view.</param>
/// <param name="modelForPartial">The model that is passed to the partial.</param>
/// <returns>An <see cref="IHtmlString"/> representation of the partial.</returns>
public IHtmlString Partial(string viewName, dynamic modelForPartial)
{
if (string.IsNullOrWhiteSpace(viewName)) throw new ArgumentNullException("viewName", "Attempted to render a non-existent view");
//this.RenderContext.Context.WriteTraceLog(sb => sb.AppendFormat("Rendered partial view {0} with type {1}", viewName, response));
try
{
var view = this.RenderContext.LocateView(viewName, modelForPartial);
var response = this.Engine.RenderView(view, modelForPartial, this.RenderContext, true);
Action<Stream> action = response.Contents;
var mem = new MemoryStream();
action.Invoke(mem);
mem.Position = 0;
var reader = new StreamReader(mem);
return new NonEncodedHtmlString(reader.ReadToEnd());
}
catch (Exception exception)
{
this.RenderContext.Context.WriteTraceLog(sb => sb.AppendFormat("Rendering partial view {0} threw exception {1}", viewName, exception.Message));
throw;
}
}
/// <summary>
/// Returns an html string composed of raw, non-encoded text.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>An <see cref="IHtmlString"/> representation of the raw text.</returns>
public IHtmlString Raw(string text)
{
return new NonEncodedHtmlString(text);
}
/// <summary>
/// Creates an anti-forgery token.
/// </summary>
/// <returns>An <see cref="IHtmlString"/> representation of the anti forgery token.</returns>
public IHtmlString AntiForgeryToken()
{
var tokenKeyValue =
this.RenderContext.GetCsrfToken();
return new NonEncodedHtmlString(String.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\"/>", tokenKeyValue.Key, tokenKeyValue.Value));
}
/// <summary>
/// Returns current culture name
/// </summary>
public string CurrentLocale
{
get { return this.RenderContext.Context.Culture.Name; }
}
/// <summary>
/// Returns current authenticated user name
/// </summary>
public IUserIdentity CurrentUser
{
get { return this.RenderContext.Context.CurrentUser; }
}
/// <summary>
/// Determines if current user is authenticated
/// </summary>
public bool IsAuthenticated
{
get { return this.RenderContext.Context.CurrentUser != null; }
}
}
}