32 lines
990 B
C#
32 lines
990 B
C#
namespace Htmx.ApiDemo.Templates;
|
|
|
|
public class Greeting : GreetingBase
|
|
{
|
|
private byte[] _userData = [];
|
|
public required string Username { init => _userData = value.ToUtf8Bytes(); }
|
|
protected override void RenderUser(HtmxRenderContext context) => context.Writer.WriteUtf8(_userData);
|
|
}
|
|
|
|
[Handler]
|
|
[MapGet("/greet/{username}")]
|
|
public static partial class GetGreetingHandler
|
|
{
|
|
public record Query(string Username);
|
|
|
|
private static ValueTask HandleAsync(
|
|
Query query,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
CancellationToken token)
|
|
{
|
|
var context = httpContextAccessor.HttpContext;
|
|
if(context is null)
|
|
throw new InvalidOperationException("HttpContext is not available.");
|
|
|
|
var template = new Greeting { Username = query.Username };
|
|
|
|
context.Response.ContentType = "text/html; charset=utf-8";
|
|
context.Response.BodyWriter.WriteHtmx(template);
|
|
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
} |