52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace StreamCinemaWeb.Layouts;
|
|
|
|
public abstract class BasicLayout
|
|
{
|
|
protected abstract string Title { get; }
|
|
|
|
protected abstract Task RenderContentAsync(HttpRequest req, TextWriter w);
|
|
|
|
public async Task<IResult> ExecuteAsync(HttpRequest req)
|
|
{
|
|
int statusCode = 200;
|
|
StringBuilder content = new StringBuilder();
|
|
TextWriter w = new StringWriter(content);
|
|
try
|
|
{
|
|
RenderHeader(Title, w);
|
|
await RenderContentAsync(req, w);
|
|
RenderFooter(w);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
content.Clear();
|
|
RenderHeader(Title + " - Error " + e.GetType().Name, w);
|
|
w.WriteLine("<h1>Error " + e.GetType().Name + "</h1><div><pre>" + HttpUtility.HtmlEncode(e.Message) + "</pre></div>");
|
|
RenderFooter(w);
|
|
statusCode = 500;
|
|
}
|
|
|
|
return Results.Content(content.ToString(), "text/html; charset=utf-8", null, statusCode);
|
|
}
|
|
|
|
private static void RenderHeader(string title, TextWriter w) {
|
|
w.WriteLine("<!doctype html>");
|
|
w.WriteLine("<html><head>");
|
|
w.WriteLine("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
|
|
w.WriteLine("<title>" + HttpUtility.HtmlEncode(title) + "</title>");
|
|
w.WriteLine("<style>");
|
|
// style go here
|
|
w.WriteLine("</style>");
|
|
w.WriteLine("</head><body>");
|
|
}
|
|
|
|
private static void RenderFooter(TextWriter w) {
|
|
w.WriteLine("</body></html>");
|
|
}
|
|
}
|