-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_static_pages.cs
201 lines (116 loc) · 3.91 KB
/
app_static_pages.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Text;
using System.Threading.Tasks;
using rmp;
using System.Web;
namespace mywebsite
{
/*
==================================================
Illustrates using a static class to render website
pages.
Note the [routemap] attributes above those methods
used to render pages. See rpm.cs for details.
==================================================
*/
public static class app_static_pages
{
private static int m_static1_render_count;
private static int m_static2_render_count;
/*
--------------------------------------------------
Constructor ...
--------------------------------------------------
*/
static app_static_pages()
{
m_static1_render_count = 0;
m_static2_render_count = 0;
}
/*
--------------------------------------------------
Given a StringBuilder, add HTML code for the start
of a page ...
--------------------------------------------------
*/
public static void page_start( StringBuilder sb, String title = "Title", Boolean link_to_home = true )
{
String s;
s = HttpUtility.HtmlEncode( title );
sb.AppendLine( $@"<html><head><title>{s}</title></head><body>" );
sb.AppendLine( @"<h1>" );
if ( link_to_home )
{
sb.AppendLine( @"<a href=""/"">Home</a> : " );
}
sb.Append( s );
sb.AppendLine( "</h1>" );
}
/*
--------------------------------------------------
Given a StringBuilder, add HTML code for the end
of a page ...
--------------------------------------------------
*/
public static void page_end( StringBuilder sb )
{
sb.AppendLine( $@"<p>{System.DateTime.UtcNow:F}</p>" );
sb.AppendLine( @"</body></html>" );
}
/*
--------------------------------------------------
Render page ...
--------------------------------------------------
*/
[routemap( "/static1" )]
public static async Task static1_render( HttpContext http_context )
{
StringBuilder sb;
sb = new StringBuilder();
// Build response ...
page_start( sb, "Static1 Page" );
m_static1_render_count++;
sb.AppendLine( $@"<p>Render count: {m_static1_render_count}</p>" );
page_end( sb );
// Send response ...
http_context.Response.ContentType = "text/html";
await http_context.Response.WriteAsync( sb.ToString() );
return;
}
/*
--------------------------------------------------
Render page with a required ID route parameter ...
--------------------------------------------------
*/
[routemap( "/static2/{id:int}" )]
public static async Task static2_render( HttpContext http_context )
{
const String k_id = "id";
int id;
StringBuilder sb;
sb = new StringBuilder();
page_start( sb, "Static2 Page" );
m_static2_render_count++;
sb.AppendLine( $@"<p>Render count: {m_static2_render_count}</p>" );
// Get ID ...
var route_data = http_context.GetRouteData();
if ( route_data is not null )
{
// Get an individual data value ...
if ( route_data.Values.ContainsKey( k_id ) )
{
id = Int32.Parse( (String)route_data.Values[ k_id ] );
// Build response ...
sb.AppendLine( $@"<p> ID: {id}</p>" );
}
}
page_end( sb );
// Send response ...
http_context.Response.ContentType = "text/html";
await http_context.Response.WriteAsync( sb.ToString() );
return;
}
}
}