- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathProgram.cs
35 lines (29 loc) · 1.63 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
usingMicrosoft.AspNetCore.Builder;
usingMicrosoft.AspNetCore.Http;
usingMicrosoft.AspNetCore.Mvc;
varweb=WebApplication.Create();
web.MapPost("/greet",([FromBody]Greetinggreet)=>Results.Json(new{Message=greet.Message+" from the server"}));
web.MapGet("/",()=>Results.Text(@"<!doctype html><html><body>
<div id=""response""></div><button type=""button"" onclick=""send()"">Click</button>
<script>
async function postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message : ""Hello World""})
});
return response.json(); // parses JSON response into native JavaScript objects
}
function send(){
postData(window.location + 'greet', { answer: 42 })
.then(data => {
document.getElementById(""response"").innerHTML = data.message;
});
}
</script>
</body></html>
","text/html"));
web.Run();
publicrecordGreeting(stringMessage);