- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathProgram.cs
126 lines (106 loc) · 3.78 KB
/
Program.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
usingHtmx;
usingMicrosoft.AspNetCore.Antiforgery;
usingMicrosoft.AspNetCore.Mvc;
varbuilder=WebApplication.CreateBuilder();
builder.Services.AddAntiforgery();
varapp=builder.Build();
app.UseAntiforgery();
app.MapGet("/",(HttpContextcontext,[FromServices]IAntiforgeryanti)=>
{
vartoken=anti.GetAndStoreTokens(context);
varhtml=$$"""
<!DOCTYPE html>
<html>
<head>
<style>
li{
cursor:pointer;
}
</style>
<meta name="htmx-config" content='{ "antiForgery": {"headerName" : "{{token.HeaderName}}", "requestToken" : "{{token.RequestToken }}" } }'>
</head>
<body>
<h1>HX-Replace-Url</h1>
<p>Click on the below links to see the response. Don't forget to check your browser url.</p>
<ul>
<li hx-get="/htmx">GET</li>
<li hx-post="/htmx">POST</li>
<li hx-put="/htmx">PUT</li>
<li hx-patch="/htmx">PATCH</li>
<li hx-delete="/htmx">DELETE</li>
</ul>
<script src="https://unpkg.com/htmx.org@2.0.0" integrity="sha384-wS5l5IKJBvK6sPTKa2WZ1js3d947pvWXbPJ1OmWfEuxLgeHcEbjUUA5i9V5ZkpCw" crossorigin="anonymous"></script>
<script>
document.addEventListener("htmx:configRequest", (evt) => {
let httpVerb = evt.detail.verb.toUpperCase();
if (httpVerb === 'GET') return;
let antiForgery = htmx.config.antiForgery;
if (antiForgery) {
// already specified on form, short circuit
if (evt.detail.parameters[antiForgery.formFieldName])
return;
if (antiForgery.headerName) {
evt.detail.headers[antiForgery.headerName]
= antiForgery.requestToken;
} else {
evt.detail.parameters[antiForgery.formFieldName]
= antiForgery.requestToken;
}
}
});
</script>
</body>
</html>
""";
returnResults.Content(html,"text/html");
});
varhtmx=app.MapGroup("/htmx").AddEndpointFilter(async(context,next)=>
{
if(context.HttpContext.Request.IsHtmx()isfalse)
returnResults.Content("");
if(context.HttpContext.Request.Method=="GET")
returnawaitnext(context);
awaitcontext.HttpContext.RequestServices.GetRequiredService<IAntiforgery>()!.ValidateRequestAsync(context.HttpContext);
returnawaitnext(context);
});
htmx.MapGet("/",(HttpRequestrequest,HttpResponseresponse)=>
{
response.Htmx(x =>
{
x.ReplaceUrl("/get");
});
returnResults.Content($"GET => {DateTime.UtcNow}");
});
htmx.MapPost("/",(HttpRequestrequest,HttpResponseresponse)=>
{
response.Htmx(x =>
{
x.ReplaceUrl("/post");
});
returnResults.Content($"POST => {DateTime.UtcNow}");
});
htmx.MapDelete("/",(HttpRequestrequest,HttpResponseresponse)=>
{
response.Htmx(x =>
{
x.ReplaceUrl("/delete");
});
returnResults.Content($"DELETE => {DateTime.UtcNow}");
});
htmx.MapPut("/",(HttpRequestrequest,HttpResponseresponse)=>
{
response.Htmx(x =>
{
x.ReplaceUrl("/put");
});
returnResults.Content($"PUT => {DateTime.UtcNow}");
});
htmx.MapPatch("/",(HttpRequestrequest,HttpResponseresponse)=>
{
response.Htmx(x =>
{
x.ReplaceUrl("/patch");
});
returnResults.Content($"PATCH => {DateTime.UtcNow}");
});
app.Run();