JsonModule.cs 747 B

1234567891011121314151617181920212223242526272829303132
  1. namespace Benchmarks
  2. {
  3. using Carter;
  4. using System.Threading.Tasks;
  5. using Utf8Json;
  6. public class JsonModule : CarterModule
  7. {
  8. private const int _bufferSize = 27;
  9. public JsonModule() : base("json")
  10. {
  11. Get("/", (req, res, routeData) =>
  12. {
  13. res.StatusCode = 200;
  14. res.ContentType = "application/json";
  15. res.ContentLength = _bufferSize;
  16. var msg = new JsonMessage { message = "Hello, World!" };
  17. JsonSerializer.Serialize(res.Body, msg);
  18. return Task.CompletedTask;
  19. });
  20. }
  21. public struct JsonMessage
  22. {
  23. public string message;
  24. }
  25. }
  26. }